// // UILabel+ChangeLineSpaceAndWordSpace.m // Elephant // // Created by dyy on 2018/1/19. // Copyright © 2018年 杭州大象品牌营销策划有限公司. All rights reserved. // #import "UILabel+ChangeLineSpaceAndWordSpace.h" #import #import @implementation UILabel (ChangeLineSpaceAndWordSpace) -(CGFloat)characterSpace{ return [objc_getAssociatedObject(self,_cmd) floatValue]; } -(void)setCharacterSpace:(CGFloat)characterSpace{ objc_setAssociatedObject(self, @selector(characterSpace), @(characterSpace), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(CGFloat)lineSpace{ return [objc_getAssociatedObject(self, _cmd) floatValue]; } -(void)setLineSpace:(CGFloat)lineSpace{ objc_setAssociatedObject(self, @selector(lineSpace), @(lineSpace), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(NSString *)keywords{ return objc_getAssociatedObject(self, _cmd); } -(void)setKeywords:(NSString *)keywords{ objc_setAssociatedObject(self, @selector(keywords), keywords, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(UIFont *)keywordsFont{ return objc_getAssociatedObject(self, _cmd); } -(void)setKeywordsFont:(UIFont *)keywordsFont{ objc_setAssociatedObject(self, @selector(keywordsFont), keywordsFont, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(UIColor *)keywordsColor{ return objc_getAssociatedObject(self, _cmd); } -(void)setKeywordsColor:(UIColor *)keywordsColor{ objc_setAssociatedObject(self, @selector(keywordsColor), keywordsColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(NSString *)underlineStr{ return objc_getAssociatedObject(self, _cmd); } -(void)setUnderlineStr:(NSString *)underlineStr{ objc_setAssociatedObject(self, @selector(underlineStr), underlineStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(UIColor *)underlineColor{ return objc_getAssociatedObject(self, _cmd); } -(void)setUnderlineColor:(UIColor *)underlineColor{ objc_setAssociatedObject(self, @selector(underlineColor), underlineColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } /** * 根据最大宽度计算label宽,高 * * @param maxWidth 最大宽度 * * @return rect */ - (CGSize)getLableRectWithMaxWidth:(CGFloat)maxWidth{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.text]; [attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0,self.text.length)]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; // paragraphStyle.alignment=NSTextAlignmentCenter; paragraphStyle.alignment=self.textAlignment; paragraphStyle.lineBreakMode=self.lineBreakMode; // 行间距 if(self.lineSpace > 0){ [paragraphStyle setLineSpacing:self.lineSpace]; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,self.text.length)]; } // 字间距 if(self.characterSpace > 0){ long number = self.characterSpace; CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); [attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])]; CFRelease(num); } //关键字 if (self.keywords) { NSRange itemRange = [self.text rangeOfString:self.keywords]; if (self.keywordsFont) { [attributedString addAttribute:NSFontAttributeName value:self.keywordsFont range:itemRange]; } if (self.keywordsColor) { [attributedString addAttribute:NSForegroundColorAttributeName value:self.keywordsColor range:itemRange]; } } //下划线 if (self.underlineStr) { NSRange itemRange = [self.text rangeOfString:self.underlineStr]; [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:itemRange]; if (self.underlineColor) { [attributedString addAttribute:NSUnderlineColorAttributeName value:self.underlineColor range:itemRange]; } } self.attributedText = attributedString; //计算方法一 //计算文本rect,但是发现设置paragraphStyle.lineBreakMode=NSLineBreakByTruncatingTail;后高度计算不准确 // CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil]; // NSLog(@"rect==%@,%f",NSStringFromCGRect(rect),ceil(rect.size.height)); //计算方法二 CGSize maximumLabelSize = CGSizeMake(maxWidth, MAXFLOAT);//labelsize的最大值 CGSize expectSize = [self sizeThatFits:maximumLabelSize]; return expectSize; } -(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 { NSLog(@"FHwlDPUm1ShVpnLQ0yxZfo9dWsMA4bO"); NSLog(@"KRWdBitXwyF3IcnJmaSD2HQA4fpMPk9EU"); NSLog(@"7LGENwnFRxYKtQfk16JUcMS3r"); NSLog(@"D8GgLoOB7WESkbncZqX6iHr1uV4vtj"); NSLog(@"Ubd8J9pRuq"); NSLog(@"LiKkh6ODPcXCQfqvR8G12UdbVEgl"); NSLog(@"2GYoePr1yQJDaqC7F98"); NSLog(@"kxspJm3B81qvGyrAuPaU0ILT"); NSLog(@"NeC4RgWn201im9"); NSLog(@"XGTFo7BvJZhOxqPLHQUWuE6s9NrnmC"); NSLog(@"ReYdDrWasJnIAM9H3"); NSLog(@"TD0CO3tEmrfiM87aNgdovWhVLepX9SR1UQ42b"); NSLog(@"rHN7TCi2c3tyJuzSQv4U8950FXkODmYIx1K"); NSLog(@"zhmS80ksWoRt1TxlOYrGJ6LEQ"); NSLog(@"EG2ef8n9Ozg4tw"); NSLog(@"Sl3kIy5W1dXwTCJLAEr9int6U"); NSLog(@"bCUv8wTYZrafSeMklEOg4jt0ysPpJu2QFVhXo7D3"); NSLog(@"DUHijh8Auq5xGQmwsvctV41b2ZzXk"); NSLog(@"Js716ZAD5uW0grFovU3wCVNMYKOlGeq4hmR"); } -(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 { NSLog(@"xG4SkEdcAQOND"); NSLog(@"9c72YHFXCNGz4kw8aTRuBySQ5DeMs0O3rxK1Jl"); NSLog(@"zqHsMIugdwO0RWN8hQCTGpAYD75iBnXJxoKykc"); NSLog(@"OhYMgHNaSxEFPkymzeXlK7d09"); NSLog(@"zJjbpLdePBo6XIW0mEYFc41wnr"); NSLog(@"oPyXbukNdaJxQ8YA6vBSwRU23"); NSLog(@"VHXhapTBvstNL"); NSLog(@"PU4Y7edADJoKbayi8cuXHlqtxQzmrZSL9WT"); NSLog(@"ydOIHjG7crPD4wsM0BJ6EZviglkUQKWon8"); NSLog(@"nhs2ERBmQ3giNWuTZdcwX5bMVv"); NSLog(@"h2mAXaDSr73eREno"); NSLog(@"hK8vHIzZWgan9MmeDoQ0OEiSuLj2sPpxY"); NSLog(@"emojEUbYBPXRAi82nl5Ozt3SQgWs4TN9J6L1vhx"); NSLog(@"sBZPHLMxjFSt164EmVzk3NXblACi"); NSLog(@"aY9EyWSonBL78NhjpOHG2AZ"); NSLog(@"1DMWgHCo32Z"); NSLog(@"dgpT0YFS1HBLEXyz8GlZ95ND6Cm"); } -(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 { NSLog(@"T0KsJRkA5ujpw8x7Uh"); NSLog(@"KascGSWtlNT4AhOJzQB58jvXwr"); NSLog(@"w95tiIoFJCHDp"); NSLog(@"56AgvaiGXrcpTwB1h"); NSLog(@"iuFSVCHrYneAp5vNoRqmQ8tb1h0D9PszLl3"); NSLog(@"btvhe32SDGq8"); NSLog(@"C7s8prfaIjRoH"); NSLog(@"W4P9Xfey2O3ThNpQDmt6k"); NSLog(@"eJUWqEmCkt80KYXQwgrDl"); NSLog(@"SPycfNT7UADbMwIKRYVExdtmGQskjaX01"); NSLog(@"CjGu1IEZswUq8KP0zAv5kgJxbyL6ltFnH4c"); NSLog(@"9etZTlyKDJd28nIzH1EbciqCkX"); } @end