【问题标题】:Different Text Attributes per Link in UITextView/UILabel SwiftUITextView/UILabel Swift 中每个链接的不同文本属性
【发布时间】:2017-04-02 12:23:30
【问题描述】:

我想在 UITextView 或 UILabel 中为每个链接设置不同的样式。我目前正在使用带有链接的 UITextViews,但我希望 URL 显示为紫色,用户名显示为黑色。我也查看了 TTTAttributedLabel,但似乎无法找到如何完成此操作。

textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
attributedString.setAttributes([NSLinkAttributeName: url, range: range)
attributedString.addAttributes([NSForegroundColorAttributeName: UIColor.purple], range: range)

在上面的示例中,这是我尝试过的,链接将是橙色的,而不是紫色的。如果我不设置 linkTextAttributes,链接将是蓝色的。如何使用每个链接的不同属性覆盖 linkTextAttributes?

【问题讨论】:

标签: ios swift hyperlink uitextview nsattributedstring


【解决方案1】:

linkTextAttributes 无法为每个单词指定文本行为。 你试过这个吗? https://github.com/rinov/RegeributedTextView

textView.addAttribute("text1", attribute: .linkColor(.orange))
textView.addAttribute("text2", attribute: .linkColor(.purple))

NSRange 是自动计算出来的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-22
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多