我有以下代码,但我的链接总是蓝色的.我怎样才能改变它们的 colored颜色 呢?

[_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)];
[_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(position, length)];
[_string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(position, length)];

_string是NSMutableAttributedString,位置和长度都很正常.

推荐答案

Swift

Updated for Swift 4.2

linkTextAttributesUITextView配合使用

textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]

在上下文中:

let attributedString = NSMutableAttributedString(string: "The site is www.google.com.")
let linkRange = (attributedString.string as NSString).range(of: "www.google.com")
attributedString.addAttribute(NSAttributedString.Key.link, value: "https://www.google.com", range: linkRange)
let linkAttributes: [NSAttributedString.Key : Any] = [
    NSAttributedString.Key.foregroundColor: UIColor.green,
    NSAttributedString.Key.underlineColor: UIColor.lightGray,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]

// textView is a UITextView
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString

Objective-C

linkTextAttributesUITextView配合使用

textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};

资料来源:this answer

this post分开始:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"username://marcelofabri_"
                         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
                                 NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;

Ios相关问答推荐

验证开发人员应用程序证书是否已在您的设备上验证

ios PWA从外部链接返回后降低了innerheight

UITableViewCell编程约束问题

当S没有身体时该如何处理

如何在不将应用程序留在SwiftUI上的情况下更改语言?

Swift-如何通过Case Let访问模型变量

如何将 Info.plist 文件添加到 Xcode for admob,错误 添加后会产生多个命令

转换在 NavigationView 中不起作用

以正确的方式在 Text() 中使用 colored颜色

搜索在 ios xamarin.forms 中不起作用

如何将私钥添加到分发证书?

有什么方法可以预填充核心数据?

如何使用情节提要重新排列 UITabBarController 项目?

在 Swift 中上传带参数的图像

如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?

如何获取 iTunes 连接团队 ID 和团队名称?

'无效更新:第 0 节中的无效行数

如何从 UIPickerView 获取选定的值

UICollectionView:必须用非零布局参数初始化

Xcode 6 进程启动失败:try 启动应用程序超时