UILabel设置多种字体、颜色
2016-05-18 10:35
531 查看
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String,try your best to test attributed string text"]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(0, 5)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(6, 12)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(19, 6)]; UILabel *attrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 320 - 40, 90)]; attrLabel.attributedText = str; attrLabel.numberOfLines = 0;
说明
NSMutableAttributedString类可以添加各种样式,常用的设置key有:NSForegroundColorAttributeName 设置前景色,也就是文本颜色
NSFontAttributeName 设置字体
NSBackgroundColorAttributeName 设置背景色
更多内容点击NSMutableAttributedString进去查看声明。
原文来自:标哥的技术博客
相关文章推荐
- easyui window refresh 刷新两次的解决方法(推荐)
- Fourth-更丰富的用户界面
- ios 中如何应对UIScrollView快速滑动(暴力用户,暴力测试)
- uilable 上面加子视图图
- 通过分类(Category)更改UINavigationItem位置,LeftBarButtonItem,RightBarButtonItem位置
- 如何判断uiscrollview是向上滚动还是向下滚动
- iOS推送:本地通知UILocalNotification
- UI进度条相关
- java中int转string该用+""还是String.valueOf
- esayui 中的 load,reload,loadData的区别
- ThreadPoolExecutor使用和思考-线程池大小设置与BlockingQueue的三种实现区别
- CUICatalog: Invalid asset name supplied: (null)
- 详解iOS App中UiTabBarController组件的基本用法
- Quirks模式
- iOS学习--UIPickerView和UIDatePicker详解
- pyautogui_pdf内容提取到excel内_3
- MSBuild .Targets 文件
- requestLocation时需要实现didFailWithError方法
- LeetCode 347. Top K Frequent Elements(最频繁的K个元素)
- UILabel自动计算行高 并且 最多显示n行