暂无描述

APAvatarImageView.m 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // APAvatarImageView.m
  3. // Avatar
  4. //
  5. // Created by Ankur Patel on 10/19/13.
  6. // Copyright (c) 2013 Patel Labs LLC. All rights reserved.
  7. //
  8. #import "APAvatarImageView.h"
  9. @interface APAvatarImageView ()
  10. - (void)draw;
  11. @end
  12. @implementation APAvatarImageView
  13. - (id)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. _cornerRadius = self.frame.size.height/2.0f;
  18. [self draw];
  19. }
  20. return self;
  21. }
  22. - (id)initWithCoder:(NSCoder *)aDecoder
  23. {
  24. self = [super initWithCoder:aDecoder];
  25. if (self) {
  26. _borderWidth = -1.0;
  27. _cornerRadius = self.frame.size.height/2.0f;
  28. [self draw];
  29. }
  30. return self;
  31. }
  32. - (id)initWithFrame:(CGRect)frame borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
  33. {
  34. self = [super initWithFrame:frame];
  35. if (self) {
  36. _borderColor = borderColor;
  37. _borderWidth = borderWidth;
  38. _cornerRadius = self.frame.size.height/2.0f;
  39. [self draw];
  40. }
  41. return self;
  42. }
  43. - (id)initWithImage:(UIImage *)image borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
  44. {
  45. self = [super initWithImage:image];
  46. if (self) {
  47. _borderColor = borderColor;
  48. _borderWidth = borderWidth;
  49. _cornerRadius = self.frame.size.height/2.0f;
  50. [self draw];
  51. }
  52. return self;
  53. }
  54. - (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage borderColor:(UIColor*)borderColor borderWidth:(float)borderWidth
  55. {
  56. self = [super initWithImage:image highlightedImage:highlightedImage];
  57. if (self) {
  58. _borderColor = borderColor;
  59. _borderWidth = borderWidth;
  60. _cornerRadius = self.frame.size.height/2.0f;
  61. [self draw];
  62. }
  63. return self;
  64. }
  65. - (void)setBorderColor:(UIColor *)borderColor
  66. {
  67. _borderColor = borderColor;
  68. [self draw];
  69. }
  70. - (void)setBorderWidth:(float)borderWidth
  71. {
  72. _borderWidth = borderWidth;
  73. [self draw];
  74. }
  75. -(void)setCornerRadius:(float)cornerRadius
  76. {
  77. _cornerRadius = cornerRadius;
  78. [self draw];
  79. }
  80. - (void)draw
  81. {
  82. CGRect frame = self.frame;
  83. if (frame.size.width != frame.size.height) {
  84. NSLog(@"Warning: Height and Width should be the same for image view");
  85. }
  86. CALayer *l = [self layer];
  87. [l setMasksToBounds:YES];
  88. [l setCornerRadius:_cornerRadius];
  89. if (_borderWidth < 0) { // Default case
  90. [l setBorderWidth:3.0];
  91. } else {
  92. [l setBorderWidth:_borderWidth];
  93. }
  94. if (_borderColor == nil) {
  95. [l setBorderColor:[[UIColor lightGrayColor] CGColor]];
  96. } else {
  97. [l setBorderColor:[_borderColor CGColor]];
  98. }
  99. }
  100. @end