label html颜色代码,设置一个label显示多种颜色,多种字体大小(示例代码)

这篇博客展示了如何使用NSAttributedString来为UILabel设置颜色和字体大小。通过创建NSMutableAttributedString并设置特定范围的颜色,实现了文本中不同部分显示不同颜色的效果。同时,文章详细列举了NSAttributedString可设置的21个属性,包括字体、颜色、背景色、连体字、间距、删除线、下划线、笔画宽度、阴影、基线偏移等,提供了丰富的文本装饰和排版选项。
摘要由CSDN通过智能技术生成

UILabel* label = [[UILabel alloc] init];

label.frame = CGRectMake(0, 100, 200, 100);

label.textColor = [UIColor blackColor];

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];

NSRange range = NSMakeRange(0, [[str string] rangeOfString:@"5"].location);

//NSRange range = NSMakeRange(0,6); //这里是设置从第一个字符到第六个字符

[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];

[label setAttributedText:str] ;

[label sizeToFit];

[self.view addSubview:label];

下面研究下AttributedString究竟可以设置哪些属性,具体来说,有以下21个:

// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12

// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色

// NSBackgroundColorAttributeName 设置字体所在区域背景颜色&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值