最近频繁的使用Lable的一系列属性,随手整理一下相关属性的详细使用方法
NSString *str = @"第一个字符字号比正常字符小,最后一个字符字号比正常字符大";
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
// lable某一个字符字号与其他字号不一样
#pragma mark - 获取一长串字符中的某一小段字符的范围可以用如下方法: - (NSRange)rangeOfString:(NSString *)searchString 注意:只适用于不会出现重复字符串的情况,出现重复字符优先显示靠近前面的字符串
[attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:6] range:[str rangeOfString:@"第一个"]];
[attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:26] range:[str rangeOfString:@"正常字符大"]];
self.lable1.attributedText = attributeStr;
NSString *str2 = @"开始的三个字是蓝色的,中间的五个字颜色是红色的,结尾的三个字是绿色的";
NSMutableAttributedString *attributeStr2 = [[NSMutableAttributedString alloc] initWithString:str2];
// lable某一个字符颜色与其他颜色不一样
[attributeStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:[str2 rangeOfString:@"开始的"]];
[attributeStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(str2.length/2-2, 5)];
[attributeStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(str2.length-3, 3)];
self.lable2.attributedText = attributeStr2;
NSString *str3 = @"¥23.05 45.50";
NSMutableAttributedString *attributeStr3 = [[NSMutableAttributedString alloc] initWithString:str3];
// lable某一段文字加删除符
[attributeStr3 addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[str3 rangeOfString:@"45.50"]];
self.lable3.attributedText = attributeStr3;
/** 综合处理 */
NSMutableAttributedString *str4 = [[NSMutableAttributedString alloc] initWithString:@"What's The Fack Thing"];
[str4 addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,6)];
[str4 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(11,4)];
[str4 addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(16,5)];
[str4 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:26] range:NSMakeRange(7,3)];
[str4 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 6)];
[str4 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(11, 4)];
[str4 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(16, 5)];
self.lable4.attributedText = str4;