样式文本_NSMutableAttributedString
2014-12-17 10:33
260 查看
有时可能会遇到这样的问题,一个label中设置的文本含有2种以上不同的格式,又不能把它拆解为两个label来显示,这时用NSMutableAttributedString可以很好的解决问题。
[objc] view
plaincopy
- (IBAction)buttonPressed:(UIButton *)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title];
// statusLabel.text = plainText;
NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc]
initWithString:plainText];
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:statusLabel.font.pointSize]
};
NSRange nameRange = [plainText rangeOfString:title];
[styledText setAttributes:attributes
range:nameRange];
statusLabel.attributedText = styledText;
}
[objc] view
plaincopy
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute: NSForegroundColorAttributeName value:[UIColor redColor]
range: NSMakeRange(10, 1)];
[label setAttributedText: text];
[objc] view
plaincopy
- (IBAction)buttonPressed:(UIButton *)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title];
// statusLabel.text = plainText;
NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc]
initWithString:plainText];
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:statusLabel.font.pointSize]
};
NSRange nameRange = [plainText rangeOfString:title];
[styledText setAttributes:attributes
range:nameRange];
statusLabel.attributedText = styledText;
}
[objc] view
plaincopy
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute: NSForegroundColorAttributeName value:[UIColor redColor]
range: NSMakeRange(10, 1)];
[label setAttributedText: text];
相关文章推荐
- NSMutableAttributedString文本样式设置以及富文本展示
- NSMutableAttributedString 文本样式 (一)
- NSMutableAttributedString/NSAttributedString 富文本设置
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- NSMutableAttributedString 富文本删除线的用法
- iOS使用NSMutableAttributedString 实现富文本(一行文本里面不同字体大小)
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- NSMutableAttributedString实现富文本 (实现不同颜色和字体、下划线)
- iOS开发富文本制作 图片和文字/NSMutableParagraphStyle/NSMutableAttributedString
- iOS - NSMutableAttributedString 富文本的实现
- NSMutableAttributedString 详细用法 实现富文本(不同颜色字体、下划线等)
- 富文本处理NSMutableAttributedString
- 富文本处理NSMutableAttributedString
- NSMutableAttributedString 自定义文字内容的样式 间距,对其 方向 间隔之类
- iOS之设置导航控制器样式以及AttributeText, NSMutableAttributedString
- iOS使用NSMutableAttributedString 实现富文本
- iOS利用NSMutableAttributedString实现富文本的方法小结
- iphone开发中NSMutableAttributedString/NSAttributedString 富文本设置
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- NSMutableAttributedString同一个label中显示不同的字体样式