您的位置:首页 > Web前端 > CSS

样式文本_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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: