iOS UILabel 增加HTML样式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bitcser/article/details/80989795
NSString *str = @"<font color=\"#6c6c6c\">满20减5 满40减15,还剩<font color=\"#ff9147\">113天";
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 50, 300, 50)];
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    label.attributedText = attrStr;
        //如果想要改变文字的字体,请在设置attributedText之后设置
    [self.view addSubview:label];

这样可以实现后台传什么样式,APP中label展示什么样式,美滋滋

猜你喜欢

转载自blog.csdn.net/bitcser/article/details/80989795