使UILabel.attributedText中的链接*非*蓝色和* not *带下划线 [英] Make link in UILabel.attributedText *not* blue and *not* underlined

查看:190
本文介绍了使UILabel.attributedText中的链接*非*蓝色和* not *带下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望OHAttributedLabel中的某些单词成为链接,但我希望它们成为蓝色以外的颜色,并且我不希望带下划线.

I want some words within my OHAttributedLabel to be links, but I want them to be colors other than blue and I don't want the underline.

这给我一个带下划线文本的蓝色链接:

This is giving me a blue link with underlined text:

 -(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{

    NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy];   

    [mutableAttributedText beginEditing];
    [mutableAttributedText addAttribute:kOHLinkAttributeName
                   value:[NSURL URLWithString:@"http://www.somewhere.net"]
                   range:range];

    [mutableAttributedText addAttribute:(id)kCTForegroundColorAttributeName
                   value:color
                   range:range];

    [mutableAttributedText addAttribute:(id)kCTUnderlineStyleAttributeName
                   value:[NSNumber numberWithInt:kCTUnderlineStyleNone]
                   range:range];
    [mutableAttributedText endEditing];


    self.label.attributedText = mutableAttributedText;

}

由于我使用的是OHAttributedLabel,因此我也尝试使用它的NSAttributedString+Attributes.h类别中的方法,但是这些方法也返回带有蓝色下划线的链接:

Since I'm using OHAttributedLabel, I also tried using the methods in it's NSAttributedString+Attributes.h category, but those return blue underlined links as well:

-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{

NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy];

[mutableAttributedText setLink:[NSURL URLWithString:@"http://www.somewhere.net"] range:range];
[mutableAttributedText setTextColor:color range:range];
[mutableAttributedText setTextUnderlineStyle:kCTUnderlineStyleNone range:range];

self.label.attributedText = mutableAttributedText;
}

如果我注释掉设置每个版本中的链接的行,则文本会根据我传入的内容进行着色-可以.似乎设置链接会覆盖此链接,然后将其变回蓝色.

If I comment out the line setting the links in each version, the text gets colored to what I pass in - that works. It just seems like setting the link is overriding this and turning it back to blue.

不幸的是,我发现的apple docs页面显示了如何将链接文本设置为蓝色并在其下划线,这正是我不需要的: https://developer.apple.com/library /content/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/ChangingAttrStrings.html

Unfortunately the apple docs page I found shows how to set the link text to blue and underline it, exactly what I don't need: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/ChangingAttrStrings.html

推荐答案

所以我最终使用了TTTAttributedLabel:

So I ended up using TTTAttributedLabel:

-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{
    NSMutableAttributedString* newTextWithLinks = [self.label.attributedText mutableCopy];
    NSURL *url = [NSURL URLWithString:@"http://www.reddit.com"];
    self.label.linkAttributes = @{NSForegroundColorAttributeName: color, 
                                   NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};
    [self.label addLinkToURL:url withRange:range];
}

我发现OHAttributedLabel实际上确实具有设置链接并声明这些链接的颜色和下划线样式的方法.但是,我希望链接根据参数使用不同的颜色. TTTAttributedLabel允许您通过为创建的每个链接设置其linkAttributes属性来实现此目的.

I found that OHAttributedLabel actually does have methods to set links and declare colors and underline styles for those links. However, I wanted the links to be different colors based on a parameter. TTTAttributedLabel allows this by letting you set it's linkAttributes property for each link you create.

这篇关于使UILabel.attributedText中的链接*非*蓝色和* not *带下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆