设置一段文字显示不同的颜色及大小等属性
?? 實(shí)現(xiàn)效果:
UIButton *valueBtn=[UIButton buttonWithType:UIButtonTypeCustom];
??? valueBtn.frame=CGRectMake(100, 100, 200, 40);
??? NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:@"昨日產(chǎn)值: 5L鮮奶"];//有空格
??? NSRange range=NSMakeRange(0, [str length]);
//設(shè)置前半部分字體顏色
??? [str addAttribute:NSForegroundColorAttributeName value:[UIColor? lightGrayColor] range:NSMakeRange(0, 5)];
//設(shè)置前半部分字體大小
??? [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0f] range:NSMakeRange(0, 5)];
//設(shè)置后半部分字體顏色
??? [str addAttribute:NSForegroundColorAttributeName value:[UIColor? colorWithRed:244/255.0f green:93/255.0f blue:97/255.0f alpha:1.0] range:NSMakeRange(5, 5)];
//設(shè)置后半字體大小
??? [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0f] range:NSMakeRange(5, 5)];
//添加下劃線
??? [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
??? [valueBtn setAttributedTitle:str forState:UIControlStateNormal];
??? [self.view addSubview:valueBtn];
?
?
#warning :1.關(guān)鍵字 NSMutableAttributedString
2.注意 NSMakeRange(a,b)中 ,參數(shù)1:是起始點(diǎn)位置下標(biāo)? 參數(shù)2:長(zhǎng)度
3.前面屬性帶AttributeName
轉(zhuǎn)載于:https://www.cnblogs.com/vegas/p/5133204.html
總結(jié)
以上是生活随笔為你收集整理的设置一段文字显示不同的颜色及大小等属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 手动用hibernate-validat
- 下一篇: iOS Xcode个人常用插件