提问人:RRSK01_11 提问时间:11/5/2023 最后编辑:RRSK01_11 更新时间:11/5/2023 访问量:33
如何使属性字符串永不换行?
How to make attributed string never wrap?
问:
我正在将 NSAttributedString 用于我的 NSTextView,以显示超链接以及其他一些字符串。当我的另一个字符串很长时,链接字符串会换行(即链接字符串会断开并移动到下一行)。如何避免它。
我将提供我所做工作的示例代码:
let displayedStringValue ="Learn More"
let url = URL(string: displayedStringValue)
let linkRange = NSRange(location: detail.count, length: displayedStringValue.count)
let fullTextString = "This is sample String" + displayedStringValue
let attributedString = NSMutableAttributedString(string: fullTextString)
let learnMoreFontColor = NSColor.Red
let linkAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: learnMoreFontColor,
]
attributedString.addAttributes(linkAttributes, range: linkRange)
attributedString.addAttribute(.link, value: url, range: linkRange)
let font = NSFont.systemFont(ofSize: 14.0)
let contentFontColor = NSColor.Red
let range = NSRange(location: 0, length: attributedString.length)
attributedString.addAttribute(.font, value: font, range: range)
let contentRange = NSRange(location: 0, length: detail.length)
attributedString.addAttribute(.foregroundColor, value: contentFontColor, range: contentRange)
myTextField.linkTextAttributes = linkAttributes
myTextField.textStorage?.setAttributedString(attributedString)
当 detailsstring 的内容较长时,了解更多字符串正在剪裁。如何避免这种情况?
答: 暂无答案
评论
NSAttributedString
NSMutableAttributedString