UILabel+Common.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // UILabel+UILabel_Common.m
  3. // CommonLibrary
  4. //
  5. // Created by AlexiChen on 14-1-18.
  6. // Copyright (c) 2014年 CommonLibrary. All rights reserved.
  7. //
  8. #import "UILabel+Common.h"
  9. @implementation UILabel (Common)
  10. + (instancetype)label
  11. {
  12. UILabel *label = [[UILabel alloc] init];
  13. label.textAlignment = NSTextAlignmentLeft;
  14. label.backgroundColor = [UIColor clearColor];
  15. label.textColor = [UIColor blackColor];
  16. return label;
  17. }
  18. + (instancetype)labelWithTitle:(NSString *)title
  19. {
  20. UILabel *label = [UILabel label];
  21. label.text = title;
  22. return label;
  23. }
  24. - (CGSize)contentSize
  25. {
  26. return [self textSizeIn:self.bounds.size];
  27. }
  28. - (CGSize)textSizeIn:(CGSize)size
  29. {
  30. NSLineBreakMode breakMode = self.lineBreakMode;
  31. UIFont *font = self.font;
  32. CGSize contentSize = CGSizeZero;
  33. // if ([IOSDeviceConfig sharedConfig].isIOS7)
  34. // {
  35. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  36. paragraphStyle.lineBreakMode = breakMode;
  37. paragraphStyle.alignment = self.textAlignment;
  38. NSDictionary* attributes = @{NSFontAttributeName:font,
  39. NSParagraphStyleAttributeName:paragraphStyle};
  40. contentSize = [self.text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
  41. // }
  42. // else
  43. // {
  44. // contentSize = [self.text sizeWithFont:font constrainedToSize:size lineBreakMode:breakMode];
  45. // }
  46. contentSize = CGSizeMake((int)contentSize.width + 1, (int)contentSize.height + 1);
  47. return contentSize;
  48. }
  49. - (CGFloat)takeLblHeight:(NSString *)content withTextFontSize:(CGFloat)mFontSize lineSpaceing:(NSInteger)lineSpaceing size:(CGSize)size
  50. {
  51. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  52. [paragraphStyle setLineSpacing:lineSpaceing];
  53. NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:content];
  54. [attributes addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:mFontSize] range:NSMakeRange(0, content.length)];
  55. [attributes addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
  56. CGSize attSize = [attributes boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
  57. self.attributedText = attributes;
  58. return attSize.height;
  59. }
  60. - (void)lblHeight:(CGFloat)lblHeight
  61. {
  62. CGRect rect = self.frame;
  63. rect.size.height = lblHeight;
  64. self.frame = rect;
  65. }
  66. //- (void)layoutInContent
  67. //{
  68. // CGSize size = [self contentSize];
  69. // CGRect rect = self.frame;
  70. // rect.size = size;
  71. // self.frame = rect;
  72. //}
  73. //
  74. @end
  75. @implementation InsetLabel
  76. - (void)drawTextInRect:(CGRect)rect
  77. {
  78. [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _contentInset)];
  79. }
  80. - (CGSize)contentSize
  81. {
  82. CGRect rect = UIEdgeInsetsInsetRect(self.bounds, _contentInset);
  83. CGSize size = [super textSizeIn:rect.size];
  84. return CGSizeMake(size.width + _contentInset.left + _contentInset.right, size.height + _contentInset.top + _contentInset.bottom);
  85. }
  86. - (CGSize)textSizeIn:(CGSize)size
  87. {
  88. size.width -= _contentInset.left + _contentInset.right;
  89. size.height -= _contentInset.top + _contentInset.bottom;
  90. CGSize textSize = [super textSizeIn:size];
  91. return CGSizeMake(textSize.width + _contentInset.left + _contentInset.right, textSize.height + _contentInset.top + _contentInset.bottom);
  92. }
  93. @end