【问题标题】:Multiple Hyperlinks in UITextView : Underlined and Bold, Bold Not workingUITextView 中的多个超链接:下划线和粗体,粗体不起作用
【发布时间】:2021-06-10 22:18:45
【问题描述】:

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

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

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 数组中,它也不起作用。

在这个问题上摸不着头脑。

【问题讨论】:

    标签: swift xcode hyperlink uitextview nsattributedstring


    【解决方案1】:

    问题是在将粗体应用于linkRange 之后,您将regular(非粗体)文本样式应用于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)    
    }
    

    【讨论】:

    • 属性的位置。非常感谢你,塔伦!
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2011-10-23
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多