RTLabel问题(粗体链接)

时间:2013-10-30 06:13:29

标签: ios iphone ipad nsstring uilabel

我使用来自https://github.com/honcheng/RTLabel的RTLabel 我需要在文本中使用链接,我使用:

moreInfoString = [NSString stringWithFormat:@"%@ <a href='%@'>%@</a>", text, httpReferense, title];
RTLabel *descriptionSourceLabel = [[RTLabel alloc] init];
descriptionSourceLabel.backgroundColor = [UIColor clearColor];
descriptionSourceLabel.delegate = self;
[descriptionSourceLabel setText:moreInfoString];

但是链接中的文字是粗体。如何取消链接中的粗体文字?

2 个答案:

答案 0 :(得分:2)

linkAttributes属性可用于链接的自定义样式。如果将linkAttributes设置为空字典,则不会对链接进行样式设置。

// Remove all link styles
descriptionSourceLabel.linkAttributes = @{};

// To change only i.e. link color
descriptionSourceLabel.linkAttributes = @{@"color": @"red"};

答案 1 :(得分:1)

我认为我的方式不是最好的,但无论如何:

- (void)render函数中我替换了:

else if ([component.tagLabel caseInsensitiveCompare:@"a"] == NSOrderedSame)
        {
            if (self.currentSelectedButtonComponentIndex==index)
            {
                if (self.selectedLinkAttributes)
                {
                    [self applyFontAttributes:self.selectedLinkAttributes toText:attrString atPosition:component.position withLength:[component.text length]];
                }
                else
                {
                        [self applyBoldStyleToText:attrString atPosition:component.position withLength:[component.text length]];
                    [self applyColor:@"#FF0000" toText:attrString atPosition:component.position withLength:[component.text length]];
                }
            }
            else
            {
                if (self.linkAttributes)
                {
                    [self applyFontAttributes:self.linkAttributes toText:attrString atPosition:component.position withLength:[component.text length]];
                }
                else
                {
                        [self applyBoldStyleToText:attrString atPosition:component.position withLength:[component.text length]];
                    [self applySingleUnderlineText:attrString atPosition:component.position withLength:[component.text length]];
                }
            }

            NSString *value = [component.attributes objectForKey:@"href"];
            value = [value stringByReplacingOccurrencesOfString:@"'" withString:@""];
            [component.attributes setObject:value forKey:@"href"];

            [links addObject:component];
        }

with(添加了linkShouldBe_regularFont属性 - 我的自定义属性,如果linkShouldBe_regularFont == YES,则字体将是常规的):

else if ([component.tagLabel caseInsensitiveCompare:@"a"] == NSOrderedSame)
        {
            if (self.currentSelectedButtonComponentIndex==index)
            {
                if (self.selectedLinkAttributes)
                {
                    [self applyFontAttributes:self.selectedLinkAttributes toText:attrString atPosition:component.position withLength:[component.text length]];
                }
                else
                {
                    if (!self.linkShouldBe_regularFont) {
                        [self applyBoldStyleToText:attrString atPosition:component.position withLength:[component.text length]];
                    }
                    [self applyColor:@"#FF0000" toText:attrString atPosition:component.position withLength:[component.text length]];
                }
            }
            else
            {
                if (self.linkAttributes)
                {
                    [self applyFontAttributes:self.linkAttributes toText:attrString atPosition:component.position withLength:[component.text length]];
                }
                else
                {
                    if (!self.linkShouldBe_regularFont) {
                        [self applyBoldStyleToText:attrString atPosition:component.position withLength:[component.text length]];
                    }
                    [self applySingleUnderlineText:attrString atPosition:component.position withLength:[component.text length]];
                }
            }

            NSString *value = [component.attributes objectForKey:@"href"];
            value = [value stringByReplacingOccurrencesOfString:@"'" withString:@""];
            [component.attributes setObject:value forKey:@"href"];

            [links addObject:component];
        }