我从 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/