swift - iOS 使用 NSAttributedString 时更改 uilabel 链接的颜色

标签 swift uikit nsattributedstring

我正在使用 uilabel 并使用 NSAttributedString 添加链接

    let attributedText = NSMutableAttributedString(string: "http://www.google.com")


    attributedText.addAttributes([NSAttributedString.Key.link: "https://www.google.com"], range: NSRange(location: 0, length: 21))
    attributedText.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], range: NSRange(location: 0, length: 21))

    label.attributedText = attributedText
    label.tintColor = UIColor.red

如何使链接变为除 UIKit 框架提供的其他默认蓝色链接颜色之外的其他颜色。

注意:我不想使用 UITextView 或 UIWebView

最佳答案

let attributedText = "http://www.google.com"
        let multipleAttributes: [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.foregroundColor: UIColor.red,
            NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue ]
        let attributeString = NSAttributedString(string: attributedText, attributes: multipleAttributes)

        // set attributed text on a UILabel
        label.attributedText = attributeString

关于swift - iOS 使用 NSAttributedString 时更改 uilabel 链接的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740414/

相关文章:

ios - 使用Cocos2D vs UIKit创建包含益智游戏的应用

swift - UITextView 滚动在 tvOS 上反转

ios - 在 UIPasteBoard 中复制 NSAttributedString

xcode - ['Firebase/Firebase.h' 找不到文件]我的应用程序可以在模拟器上运行,但不能在我的 iphone 6 plus 上运行

ios - 如何为页面 View Controller 提供 "Cover Vertical"过渡样式

ios - 具有静态内容的 UIPageViewController

cocoa - NSAttributedString initWithHTML 字符编码不正确?

swift - 在 NSTextView 下面添加一个 NSVisualEffectView

swift - 试图计算时差,但收到否定答案

iphone - iPhone 模式下 iPad 上的 Jagged Quartz 2D 渲染