DTAttributedLabel addAttribute不起作用

时间:2015-11-10 07:26:26

标签: ios objective-c nsattributedstring nsmutableattributedstring dtcoretext

我使用DTAttributedLabel解码一些Html标签,我希望标签有两种颜色。当我使用UILabel时它工作正常,但是当我将它更改为DTAttributedLabel时,颜色没有显示。任何帮助将不胜感激。

- (void)loadData:(ArticleModel *)data andTypeName:(NSString *)typeName{
    NSString *titleText;
    titleText = [NSString stringWithFormat:@"[%@] %@", typeName, data.title];

    NSData *titleData = [titleText dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableAttributedString *attrTitleStr = [[[[DTHTMLAttributedStringBuilder alloc]
                                 initWithHTML:titleData
                                 options:@{
                                           DTDefaultFontSize:@15
                                           }
                                 documentAttributes:nil]
                                generatedAttributedString] mutableCopy];

//here I add the colors
    [attrTitleStr addAttribute:NSForegroundColorAttributeName value:KCOLOR_BLUE_0066FF range:NSMakeRange(0, typeName.length + 2)];
    [attrTitleStr addAttribute:NSForegroundColorAttributeName value:KCOLOR_RED_6D2C1D range:NSMakeRange(typeName.length + 2, attrTitleStr.length - typeName.length - 2)];

    _titleLabel = [[DTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 400, 200)];
    [_titleLabel setAttributedString:attrTitleStr];}

0 个答案:

没有答案
相关问题