设置UILabel上面的文字显示不同的字体和颜色
来源:互联网 发布:手机桌面归类软件 编辑:程序博客网 时间:2024/05/16 05:34
/**************************************显示内容是英文***********************************************/
// 创建显示英文的Label初始化并确定位置
UILabel *labelofEN = [[UILabelalloc]initWithFrame:CGRectMake(0,200, [[UIScreenmainScreen]bounds].size.width,100)];
// 构造字符串
NSMutableAttributedString *str1 = [[NSMutableAttributedStringalloc]initWithString:@"Hello World China Is Strong"];
// 为字符串中的字符添加不同的颜色
[str1 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0,11)];
[str1 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(12,15)];
// 为字符串中的字符添加不同的字体
[str1 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Arial-BoldItalicMT"size:30.0]range:NSMakeRange(0,11)];
[str1 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue-Bold"size:30.0]range:NSMakeRange(12,15)];
// Label的显示内容
labelofEN.attributedText = str1;
// Label的内容居中显示
labelofEN.textAlignment =NSTextAlignmentCenter;
// 把Label展示到画布上
[self.viewaddSubview:labelofEN];
/**************************************显示内容是中文***********************************************/
// 创建显示中文的Label初始化并确定位置
UILabel *labelofCH = [[UILabelalloc]initWithFrame:CGRectMake(0,300, [[UIScreenmainScreen]bounds].size.width,100)];
// 构造字符串
NSMutableAttributedString *str2 = [[NSMutableAttributedStringalloc]initWithString:@"我爱中国"];
// 为字符串中的字符添加不同的颜色
[str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0,1)];
[str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(1,1)];
[str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorpurpleColor]range:NSMakeRange(2,1)];
[str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColororangeColor]range:NSMakeRange(3,1)];
// 为字符串中的字符添加不同的字体
[str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Arial-BoldItalicMT"size:30.0]range:NSMakeRange(0,1)];
[str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue-Bold" size:30.0]range:NSMakeRange(1,1)];
[str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Courier-BoldOblique"size:30.0]range:NSMakeRange(2,1)];
[str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"American Typewriter"size:30.0]range:NSMakeRange(3,1)];
// Label的显示内容
labelofCH.attributedText = str2;
// Label的内容居中显示
labelofCH.textAlignment =NSTextAlignmentCenter;
// 把Label展示到画布上
[self.viewaddSubview:labelofCH];
效果图:
- 设置UILabel上面的文字显示不同的字体和颜色
- UILabel显示不同的颜色和字体
- UILabel显示不同的字体和颜色
- UILabel显示不同的字体和颜色
- 渲染自定义格式字符串的UILabel(同一个lable的不同文字可以显示不同的颜色和字体)
- iOS开发笔记--UILabel显示不同的字体和颜色
- iOS 在UILabel显示不同的字体和颜色
- iOS 在UILabel显示不同的字体和颜色
- iOS 在UILabel显示不同的字体和颜色(转)
- iOS 在UILabel显示不同的字体和颜色
- iOS 在UILabel显示不同的字体和颜色
- iOS开发笔记--UILabel显示不同的字体和颜色
- iOS 在UILabel显示不同的字体和颜色
- iOS 在UILabel显示不同的字体和颜色(转)
- iOS 在UILabel显示不同的字体和颜色 NSMutableAttributedString
- iOS 在UILabel显示不同的字体和颜色(转)
- iOS 在UILabel显示不同的字体和颜色
- iOS 在UILabel显示不同的字体和颜色
- Java中类HashSet<E>和类LinkedHashSet<E>的区别
- facebook的Android调试工具Stetho介绍和使用注意事项
- VC中ListCtrl控件的使用
- HDU 1164 Eddy's research I 数论
- 把OkHttp的网络获取方法写在公共类里
- 设置UILabel上面的文字显示不同的字体和颜色
- JAXB注解小示例
- Fragment页面之间传值、基本例子演示、生命周期、兼容性
- 配置Java环境变量
- Unity3D 摄像机平滑跟随角色
- sublime的使用
- dSYM 文件
- REST简单介绍
- Java中反射机制案例分析