省钱达人老版本

UILabel+ChangeLineSpaceAndWordSpace.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. @end