口袋优选

UILabel+ChangeLineSpaceAndWordSpace.m 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // UILabel+ChangeLineSpaceAndWordSpace.m
  3. // Elephant
  4. //
  5. // Created by dyy on 2018/1/19.
  6. // Copyright © 2018年 杭州大象品牌营销策划有限公司. All rights reserved.
  7. //
  8. #import "UILabel+ChangeLineSpaceAndWordSpace.h"
  9. #import <objc/runtime.h>
  10. #import <CoreText/CoreText.h>
  11. @implementation UILabel (ChangeLineSpaceAndWordSpace)
  12. - (void)changeLineSpaceWithSpace:(float)space {
  13. NSString *labelText = self.text;
  14. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
  15. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  16. [paragraphStyle setLineSpacing:space];
  17. [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
  18. self.attributedText = attributedString;
  19. [self sizeToFit];
  20. }
  21. -(CGFloat)characterSpace{
  22. return [objc_getAssociatedObject(self,_cmd) floatValue];
  23. }
  24. -(void)setCharacterSpace:(CGFloat)characterSpace{
  25. objc_setAssociatedObject(self, @selector(characterSpace), @(characterSpace), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  26. }
  27. -(CGFloat)lineSpace{
  28. return [objc_getAssociatedObject(self, _cmd) floatValue];
  29. }
  30. -(void)setLineSpace:(CGFloat)lineSpace{
  31. objc_setAssociatedObject(self, @selector(lineSpace), @(lineSpace), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  32. }
  33. -(NSString *)keywords{
  34. return objc_getAssociatedObject(self, _cmd);
  35. }
  36. -(void)setKeywords:(NSString *)keywords{
  37. objc_setAssociatedObject(self, @selector(keywords), keywords, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  38. }
  39. -(UIFont *)keywordsFont{
  40. return objc_getAssociatedObject(self, _cmd);
  41. }
  42. -(void)setKeywordsFont:(UIFont *)keywordsFont{
  43. objc_setAssociatedObject(self, @selector(keywordsFont), keywordsFont, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  44. }
  45. -(UIColor *)keywordsColor{
  46. return objc_getAssociatedObject(self, _cmd);
  47. }
  48. -(void)setKeywordsColor:(UIColor *)keywordsColor{
  49. objc_setAssociatedObject(self, @selector(keywordsColor), keywordsColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  50. }
  51. -(NSString *)underlineStr{
  52. return objc_getAssociatedObject(self, _cmd);
  53. }
  54. -(void)setUnderlineStr:(NSString *)underlineStr{
  55. objc_setAssociatedObject(self, @selector(underlineStr), underlineStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  56. }
  57. -(UIColor *)underlineColor{
  58. return objc_getAssociatedObject(self, _cmd);
  59. }
  60. -(void)setUnderlineColor:(UIColor *)underlineColor{
  61. objc_setAssociatedObject(self, @selector(underlineColor), underlineColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  62. }
  63. /**
  64. * 根据最大宽度计算label宽,高
  65. *
  66. * @param maxWidth 最大宽度
  67. *
  68. * @return rect
  69. */
  70. - (CGSize)getLableRectWithMaxWidth:(CGFloat)maxWidth{
  71. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.text];
  72. [attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0,self.text.length)];
  73. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  74. // paragraphStyle.alignment=NSTextAlignmentCenter;
  75. paragraphStyle.alignment=self.textAlignment;
  76. paragraphStyle.lineBreakMode=self.lineBreakMode;
  77. // 行间距
  78. if(self.lineSpace > 0){
  79. [paragraphStyle setLineSpacing:self.lineSpace];
  80. [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,self.text.length)];
  81. }
  82. // 字间距
  83. if(self.characterSpace > 0){
  84. long number = self.characterSpace;
  85. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
  86. [attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])];
  87. CFRelease(num);
  88. }
  89. //关键字
  90. if (self.keywords) {
  91. NSRange itemRange = [self.text rangeOfString:self.keywords];
  92. if (self.keywordsFont) {
  93. [attributedString addAttribute:NSFontAttributeName value:self.keywordsFont range:itemRange];
  94. }
  95. if (self.keywordsColor) {
  96. [attributedString addAttribute:NSForegroundColorAttributeName value:self.keywordsColor range:itemRange];
  97. }
  98. }
  99. //下划线
  100. if (self.underlineStr) {
  101. NSRange itemRange = [self.text rangeOfString:self.underlineStr];
  102. [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:itemRange];
  103. if (self.underlineColor) {
  104. [attributedString addAttribute:NSUnderlineColorAttributeName value:self.underlineColor range:itemRange];
  105. }
  106. }
  107. self.attributedText = attributedString;
  108. //计算方法一
  109. //计算文本rect,但是发现设置paragraphStyle.lineBreakMode=NSLineBreakByTruncatingTail;后高度计算不准确
  110. // CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
  111. // NSLog(@"rect==%@,%f",NSStringFromCGRect(rect),ceil(rect.size.height));
  112. //计算方法二
  113. CGSize maximumLabelSize = CGSizeMake(maxWidth, MAXFLOAT);//labelsize的最大值
  114. CGSize expectSize = [self sizeThatFits:maximumLabelSize];
  115. return expectSize;
  116. }
  117. @end