swift - UITextView 中的多个超链接 : Underlined and Bold, 粗体不起作用

标签 swift xcode hyperlink uitextview nsattributedstring

我从 SO 上的帖子中找到了这段代码,我找不到原始链接,所以我深表歉意。

本质上,这段代码适用于我需要在 UITextView 中执行的所有操作,多个带有下划线的链接。但是,当我尝试添加粗体属性时,我无法使其正常工作。

import UIKit

extension UITextView {

  func buildLink(originalText: String, hyperLinks: [String: String]) {
    
    let style = NSMutableParagraphStyle()
    style.alignment = .left
    let attributedOriginalText = NSMutableAttributedString(string: originalText)

    for (hyperLink, urlString) in hyperLinks {
        let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
        let fullRange = NSRange(location: 0, length: attributedOriginalText.length)
        attributedOriginalText.addAttribute(.font, value: UIFont(name: "RobotoCondensed-Bold", size: 12.0)!, range: linkRange) /// this is the non functioning line
        attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "RobotoCondensed-Regular", size 12.0)!, range: fullRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.darkText, range: fullRange)
    }

    self.linkTextAttributes = [
        NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
    ]
    self.attributedText = attributedOriginalText
  }
}

如果我还将粗体属性放入 linkTextAttributes 数组中,它也不起作用。

对此感到困惑。

最佳答案

问题在于,在将粗体应用于 linkRange 后,您将常规(非粗体)文本样式应用于 fullRange .

for (hyperLink, urlString) in hyperLinks {
    let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
    let fullRange = NSRange(location: 0, length: attributedOriginalText.length)

    /// Here the correct attribute is applied for linkRange
    attributedOriginalText.addAttribute(.font, value: UIFont(name: "RobotoCondensed-Bold", size: 12.0)!, range: linkRange) /// this is the non functioning line
    attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
    
    attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
    
    /// Here what was done correctly above gets overwritten
    attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "RobotoCondensed-Regular", size 12.0)!, range: fullRange)
    attributedOriginalText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.darkText, range: fullRange)
}

这可以简化为以下 -

let attributedOriginalText = NSMutableAttributedString(string: originalText)

/// Apply the normal text style to whole string at once
let fullRange = NSRange(location: 0, length: attributedOriginalText.length)
attributedOriginalText.addAttribute(.paragraphStyle, value: style, range: fullRange)
attributedOriginalText.addAttribute(.font, value: UIFont(name: "RobotoCondensed-Regular", size 12.0)!, range: fullRange)
attributedOriginalText.addAttribute(.foregroundColor, value: UIColor.darkText, range: fullRange)

/// Apply the link attributes at specified link ranges
for (hyperLink, urlString) in hyperLinks {
    let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
    attributedOriginalText.addAttribute(.font, value: UIFont(name: "RobotoCondensed-Bold", size: 12.0)!, range: linkRange)
    attributedOriginalText.addAttribute(.link, value: urlString, range: linkRange)    
}

关于swift - UITextView 中的多个超链接 : Underlined and Bold, 粗体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67929083/

相关文章:

javascript - 如何区分一个链接是否会触发文件下载或跳转到其他地方,例如新的URL

ios - 嵌套参数化闭包参数异常

ios - 使用 swift 并排合并 2 个视频

c++ - Open GL 和 GLUT 在 10.9 中被弃用

ios - 是否可以在 UITextview 链接选择上设置清除颜色?

javascript - 根据点击的超链接显示/隐藏表单

ios - UITableViewCell 委托(delegate) indexPath

iOS项目的Swift Building IP 4地址类

iphone - 无法在 Xcode 中选择文件所有者

ios - 在 ARKit 体验中计算旋转 SCNPlane 的 4 个顶点位置