iOS 字体
来源:互联网 发布:电脑上淘宝直播 编辑:程序博客网 时间:2024/06/11 18:48
原文地址:http://my.oschina.net/CarlHuang/blog/138363
在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的attributedText, 使用不同颜色和不同字体的字符串,我们可以使用NSAttributedText 和 NSMutableAttributedText 类来实现。
.h 文件
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UILabel *attrLabel;
- (IBAction)next:(id)sender;
@end
self.title = @
"For iOS 6 & later"
;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@
"Using NSAttributed String"
];
[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-BoldItalicMT"
size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@
"HelveticaNeue-Bold"
size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@
"Courier-BoldOblique"
size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;
效果图:
如果想在iOS6.0以前版本实现这个效果,需要使用到一个第三方库TTTAttributedLabel,同时还有导入CoreText.frame框架.
增加下划线:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 100)];
- ios字体
- iOS字体
- IOS字体
- iOS 字体
- IOS字体
- IOS字体
- iOS 字体
- iOS:字体
- iOS 字体
- IOS 字体
- ios字体
- iOS 字体
- iOS 设置字体 自定义字体
- iOS 设置字体 自定义字体
- iOS 设置字体 自定义字体
- 获取IOS设备字体
- [iOS] UIFont 设置字体
- ios 漂亮字体
- iOS关于聊天消息提醒铃声
- scp使用加密算法报错unknown cipher type
- Java NIO使用及原理分析 (一)
- 栈的链式存储实现
- java对redis的基本操作
- iOS 字体
- [LeetCode] Minimum Path Sum
- java 获取map中所有的key值
- Masonry介绍与使用实践(快速上手Autolayout)
- linux创建和root一样权限的用户
- Ubuntu/linux下忘记MySQL root密码解决方法
- Linux系统编程_8_进程控制之fork_wait_waitpid函数
- 查看Linux的磁盘使用情况
- 微博feed系统的推(push)模式和拉(pull)模式和时间分区拉模式架构探讨