Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. - (id)init
  2. {
  3. self = [super init];
  4.  
  5. self.translatesAutoresizingMaskIntoConstraints = NO;
  6. self.numberOfLines = 2;
  7. self.backgroundColor = UIColorFromARGB(0x99000000);
  8. self.textColor = [UIColor whiteColor];
  9. self.font = [UIFont boldSystemFontOfSize:14.0f];
  10.  
  11. return self;
  12. }
  13.  
  14. - (void)drawTextInRect:(CGRect)rect {
  15. UIEdgeInsets insets = {2, 7, 2, 7};
  16. return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
  17. }
  18.  
  19. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
  20. {
  21. UIEdgeInsets insets = {2, 7, 2, 7};
  22. return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,insets) limitedToNumberOfLines:numberOfLines];
  23. }
  24.  
  25. @implementation InsetLabel
  26.  
  27. - (void) setInsets:(UIEdgeInsets)insets
  28. {
  29. _insets = insets ;
  30. [self invalidateIntrinsicContentSize] ;
  31. }
  32.  
  33. - (void)drawTextInRect:(CGRect)rect
  34. {
  35. return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
  36. }
  37.  
  38. - (void)resizeHeightToFitText
  39. {
  40. CGRect frame = [self bounds];
  41. CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);
  42.  
  43. CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
  44. frame.size.height = newSize.height + self.insets.top + self.insets.bottom;
  45. self.frame = frame;
  46. }
  47.  
  48. - (CGSize) intrinsicContentSize
  49. {
  50. CGSize superSize = [super intrinsicContentSize] ;
  51. superSize.height += self.insets.top + self.insets.bottom ;
  52. superSize.width += self.insets.left + self.insets.right ;
  53. return superSize ;
  54. }
  55.  
  56. @end
  57.  
  58. @implementation InsetLabel
  59.  
  60. - (void) setInsets:(UIEdgeInsets)insets
  61. {
  62. _insets = insets ;
  63. [self invalidateIntrinsicContentSize] ;
  64. }
  65.  
  66. - (void)drawTextInRect:(CGRect)rect
  67. {
  68. return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
  69. }
  70.  
  71. - (void)resizeHeightToFitText
  72. {
  73. CGRect frame = [self frame];
  74. CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);
  75.  
  76. CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
  77. frame.size.height = newSize.height + self.insets.top + self.insets.bottom;
  78. self.frame = frame;
  79. }
  80.  
  81. - (CGSize) intrinsicContentSize
  82. {
  83. CGSize superSize = [super intrinsicContentSize] ;
  84. superSize.height += self.insets.top + self.insets.bottom ;
  85. superSize.width += self.insets.left + self.insets.right ;
  86. return superSize ;
  87. }
  88. - (void)layoutSubviews
  89. {
  90. [super layoutSubviews];
  91. [self resizeHeightToFitText];
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement