1

我使用来自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];

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

4

2 回答 2

2

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

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

// To change only i.e. link color
descriptionSourceLabel.linkAttributes = @{@"color": @"red"};
于 2014-07-02T11:27:05.373 回答
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];
        }

与(添加了 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];
        }
于 2013-10-30T06:49:46.103 回答