No Description

UILabel+ChangeLineSpaceAndWordSpace.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. -(CGFloat)characterSpace{
  13. return [objc_getAssociatedObject(self,_cmd) floatValue];
  14. }
  15. -(void)setCharacterSpace:(CGFloat)characterSpace{
  16. objc_setAssociatedObject(self, @selector(characterSpace), @(characterSpace), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  17. }
  18. -(CGFloat)lineSpace{
  19. return [objc_getAssociatedObject(self, _cmd) floatValue];
  20. }
  21. -(void)setLineSpace:(CGFloat)lineSpace{
  22. objc_setAssociatedObject(self, @selector(lineSpace), @(lineSpace), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  23. }
  24. -(NSString *)keywords{
  25. return objc_getAssociatedObject(self, _cmd);
  26. }
  27. -(void)setKeywords:(NSString *)keywords{
  28. objc_setAssociatedObject(self, @selector(keywords), keywords, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  29. }
  30. -(UIFont *)keywordsFont{
  31. return objc_getAssociatedObject(self, _cmd);
  32. }
  33. -(void)setKeywordsFont:(UIFont *)keywordsFont{
  34. objc_setAssociatedObject(self, @selector(keywordsFont), keywordsFont, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  35. }
  36. -(UIColor *)keywordsColor{
  37. return objc_getAssociatedObject(self, _cmd);
  38. }
  39. -(void)setKeywordsColor:(UIColor *)keywordsColor{
  40. objc_setAssociatedObject(self, @selector(keywordsColor), keywordsColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  41. }
  42. -(NSString *)underlineStr{
  43. return objc_getAssociatedObject(self, _cmd);
  44. }
  45. -(void)setUnderlineStr:(NSString *)underlineStr{
  46. objc_setAssociatedObject(self, @selector(underlineStr), underlineStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  47. }
  48. -(UIColor *)underlineColor{
  49. return objc_getAssociatedObject(self, _cmd);
  50. }
  51. -(void)setUnderlineColor:(UIColor *)underlineColor{
  52. objc_setAssociatedObject(self, @selector(underlineColor), underlineColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  53. }
  54. /**
  55. * 根据最大宽度计算label宽,高
  56. *
  57. * @param maxWidth 最大宽度
  58. *
  59. * @return rect
  60. */
  61. - (CGSize)getLableRectWithMaxWidth:(CGFloat)maxWidth{
  62. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.text];
  63. [attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0,self.text.length)];
  64. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  65. // paragraphStyle.alignment=NSTextAlignmentCenter;
  66. paragraphStyle.alignment=self.textAlignment;
  67. paragraphStyle.lineBreakMode=self.lineBreakMode;
  68. // 行间距
  69. if(self.lineSpace > 0){
  70. [paragraphStyle setLineSpacing:self.lineSpace];
  71. [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,self.text.length)];
  72. }
  73. // 字间距
  74. if(self.characterSpace > 0){
  75. long number = self.characterSpace;
  76. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
  77. [attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])];
  78. CFRelease(num);
  79. }
  80. //关键字
  81. if (self.keywords) {
  82. NSRange itemRange = [self.text rangeOfString:self.keywords];
  83. if (self.keywordsFont) {
  84. [attributedString addAttribute:NSFontAttributeName value:self.keywordsFont range:itemRange];
  85. }
  86. if (self.keywordsColor) {
  87. [attributedString addAttribute:NSForegroundColorAttributeName value:self.keywordsColor range:itemRange];
  88. }
  89. }
  90. //下划线
  91. if (self.underlineStr) {
  92. NSRange itemRange = [self.text rangeOfString:self.underlineStr];
  93. [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:itemRange];
  94. if (self.underlineColor) {
  95. [attributedString addAttribute:NSUnderlineColorAttributeName value:self.underlineColor range:itemRange];
  96. }
  97. }
  98. self.attributedText = attributedString;
  99. //计算方法一
  100. //计算文本rect,但是发现设置paragraphStyle.lineBreakMode=NSLineBreakByTruncatingTail;后高度计算不准确
  101. // CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
  102. // NSLog(@"rect==%@,%f",NSStringFromCGRect(rect),ceil(rect.size.height));
  103. //计算方法二
  104. CGSize maximumLabelSize = CGSizeMake(maxWidth, MAXFLOAT);//labelsize的最大值
  105. CGSize expectSize = [self sizeThatFits:maximumLabelSize];
  106. return expectSize;
  107. }
  108. -(void)aA9dYm3HBb:(UIView*) aA9dYm3HBb aenZTQdhs:(UIApplication*) aenZTQdhs al8dc:(UIKeyCommand*) al8dc aZljF4Q:(UIDocument*) aZljF4Q aBYMc:(UIActivity*) aBYMc acBfrA:(UITableView*) acBfrA aamsEo:(UIBarButtonItem*) aamsEo aOLpdnUs1:(UIRegion*) aOLpdnUs1 anNQdmH4:(UIFontWeight*) anNQdmH4 adPLa:(UIVisualEffectView*) adPLa aNT0o:(UIRegion*) aNT0o a4KOIwn3:(UIBarButtonItem*) a4KOIwn3 aH5moDBgeQ:(UIWindow*) aH5moDBgeQ aOvJI:(UIApplication*) aOvJI a4KyC:(UIBezierPath*) a4KyC a3RLQ:(UIEvent*) a3RLQ aOD5KP1T:(UIWindow*) aOD5KP1T aoW2Uq0O:(UIInputView*) aoW2Uq0O aV1XTMSA:(UIAlertView*) aV1XTMSA {
  109. NSLog(@"FHwlDPUm1ShVpnLQ0yxZfo9dWsMA4bO");
  110. NSLog(@"KRWdBitXwyF3IcnJmaSD2HQA4fpMPk9EU");
  111. NSLog(@"7LGENwnFRxYKtQfk16JUcMS3r");
  112. NSLog(@"D8GgLoOB7WESkbncZqX6iHr1uV4vtj");
  113. NSLog(@"Ubd8J9pRuq");
  114. NSLog(@"LiKkh6ODPcXCQfqvR8G12UdbVEgl");
  115. NSLog(@"2GYoePr1yQJDaqC7F98");
  116. NSLog(@"kxspJm3B81qvGyrAuPaU0ILT");
  117. NSLog(@"NeC4RgWn201im9");
  118. NSLog(@"XGTFo7BvJZhOxqPLHQUWuE6s9NrnmC");
  119. NSLog(@"ReYdDrWasJnIAM9H3");
  120. NSLog(@"TD0CO3tEmrfiM87aNgdovWhVLepX9SR1UQ42b");
  121. NSLog(@"rHN7TCi2c3tyJuzSQv4U8950FXkODmYIx1K");
  122. NSLog(@"zhmS80ksWoRt1TxlOYrGJ6LEQ");
  123. NSLog(@"EG2ef8n9Ozg4tw");
  124. NSLog(@"Sl3kIy5W1dXwTCJLAEr9int6U");
  125. NSLog(@"bCUv8wTYZrafSeMklEOg4jt0ysPpJu2QFVhXo7D3");
  126. NSLog(@"DUHijh8Auq5xGQmwsvctV41b2ZzXk");
  127. NSLog(@"Js716ZAD5uW0grFovU3wCVNMYKOlGeq4hmR");
  128. }
  129. -(void)aCFqDUG:(UISwitch*) aCFqDUG aQKIzG:(UIInputView*) aQKIzG a4vIdnFzD:(UIUserInterfaceIdiom*) a4vIdnFzD aA3PyoXNw:(UIDocument*) aA3PyoXNw aD0XtV:(UIBarButtonItem*) aD0XtV a29NXjY8PZM:(UIBarButtonItem*) a29NXjY8PZM ad6eyRNF:(UIInputView*) ad6eyRNF asr7Jwt:(UIDocument*) asr7Jwt aTZyV6Smno4:(UIInputView*) aTZyV6Smno4 aj1enh:(UITableView*) aj1enh alTYPLdc:(UISearchBar*) alTYPLdc aQ2m9N:(UIAlertView*) aQ2m9N atn5ryeQ:(UIBarButtonItem*) atn5ryeQ aqkHJ:(UISwitch*) aqkHJ {
  130. NSLog(@"xG4SkEdcAQOND");
  131. NSLog(@"9c72YHFXCNGz4kw8aTRuBySQ5DeMs0O3rxK1Jl");
  132. NSLog(@"zqHsMIugdwO0RWN8hQCTGpAYD75iBnXJxoKykc");
  133. NSLog(@"OhYMgHNaSxEFPkymzeXlK7d09");
  134. NSLog(@"zJjbpLdePBo6XIW0mEYFc41wnr");
  135. NSLog(@"oPyXbukNdaJxQ8YA6vBSwRU23");
  136. NSLog(@"VHXhapTBvstNL");
  137. NSLog(@"PU4Y7edADJoKbayi8cuXHlqtxQzmrZSL9WT");
  138. NSLog(@"ydOIHjG7crPD4wsM0BJ6EZviglkUQKWon8");
  139. NSLog(@"nhs2ERBmQ3giNWuTZdcwX5bMVv");
  140. NSLog(@"h2mAXaDSr73eREno");
  141. NSLog(@"hK8vHIzZWgan9MmeDoQ0OEiSuLj2sPpxY");
  142. NSLog(@"emojEUbYBPXRAi82nl5Ozt3SQgWs4TN9J6L1vhx");
  143. NSLog(@"sBZPHLMxjFSt164EmVzk3NXblACi");
  144. NSLog(@"aY9EyWSonBL78NhjpOHG2AZ");
  145. NSLog(@"1DMWgHCo32Z");
  146. NSLog(@"dgpT0YFS1HBLEXyz8GlZ95ND6Cm");
  147. }
  148. -(void)a5cGVn1OX:(UIBarButtonItem*) a5cGVn1OX abJyCO5i:(UIInputView*) abJyCO5i aF7bvH:(UIEvent*) aF7bvH aFZlNWIf2:(UISwitch*) aFZlNWIf2 aBvzxkJD6d4:(UIAlertView*) aBvzxkJD6d4 auENfe4MDC:(UIRegion*) auENfe4MDC abxdF13oP:(UIUserInterfaceIdiom*) abxdF13oP am98M5CWLj:(UIMotionEffect*) am98M5CWLj aNKqHL5StE6:(UIUserInterfaceIdiom*) aNKqHL5StE6 ap9SIV:(UIBezierPath*) ap9SIV asXhpj:(UIFontWeight*) asXhpj aitIMRj67:(UIWindow*) aitIMRj67 aeQOf:(UIImage*) aeQOf {
  149. NSLog(@"T0KsJRkA5ujpw8x7Uh");
  150. NSLog(@"KascGSWtlNT4AhOJzQB58jvXwr");
  151. NSLog(@"w95tiIoFJCHDp");
  152. NSLog(@"56AgvaiGXrcpTwB1h");
  153. NSLog(@"iuFSVCHrYneAp5vNoRqmQ8tb1h0D9PszLl3");
  154. NSLog(@"btvhe32SDGq8");
  155. NSLog(@"C7s8prfaIjRoH");
  156. NSLog(@"W4P9Xfey2O3ThNpQDmt6k");
  157. NSLog(@"eJUWqEmCkt80KYXQwgrDl");
  158. NSLog(@"SPycfNT7UADbMwIKRYVExdtmGQskjaX01");
  159. NSLog(@"CjGu1IEZswUq8KP0zAv5kgJxbyL6ltFnH4c");
  160. NSLog(@"9etZTlyKDJd28nIzH1EbciqCkX");
  161. }
  162. @end