更改html中的默认字体颜色创建NSAttributedString

时间:2015-12-08 07:33:14

标签: ios nsattributedstring

“123”和“789”默认颜色为黑色,但我想换成另一种颜色,我该怎么办呢?

TTTAttributedLabel * cmdTextView = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(200, 100, 100, 30)];
cmdTextView.backgroundColor = [UIColor lightGrayColor];
cmdTextView.textColor = [UIColor redColor];
[self.view addSubview:cmdTextView];

NSString *s = @"123<font color='#FFFFFF'>456</font>789" ;
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithData:[s dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
[cmdTextView setText:attribute];

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。希望可以帮助你

NSString *s1 = [NSString stringWithFormat:"<span style=\"color:Your color\">%@</span>", s];