gpt4 book ai didi

ios - 在 UILabel.attributedText *not* 蓝色和 *not* 下划线

转载 作者:IT老高 更新时间:2023-10-28 11:48:39 25 4
gpt4 key购买 nike

我希望我的 OHAttributedLabel 中的一些单词是链接,但我希望它们是蓝色以外的颜色,并且我不想要下划线。

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

 -(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 类别中的方法,但这些方法也返回蓝色下划线链接:

-(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;
}

如果我注释掉在每个版本中设置链接的行,文本会根据我传入的内容着色 - 这很有效。似乎设置链接会覆盖它并将其变回蓝色。

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

最佳答案

所以我最终使用了 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 属性。

关于ios - 在 UILabel.attributedText *not* 蓝色和 *not* 下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15381028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com