读取所有 NSAttributedString 属性及其有效范围
Read all NSAttributedString attributes with their effective range
我正在做一个项目,需要在 textview 中找到粗体字的范围并替换它们的颜色,我已经尝试了以下但没有成功。
.enumerateAttribute (NSFontAttributeName, in:NSMakeRange(0, descriptionTextView.attributedText.length), options:.longestEffectiveRangeNotRequired) { value, range, stop in
}
传递给 enumerateAttribute
和 NSFontAttributeName
闭包的 value
参数表示绑定到 range
的 UIFont
。因此,您只需要检查字体是否为粗体并收集范围即可。
//Find ranges of bold words.
let attributedText = descriptionTextView.attributedText!
var boldRanges: [NSRange] = []
attributedText.enumerateAttribute(NSFontAttributeName, in: NSRange(0..<attributedText.length), options: .longestEffectiveRangeNotRequired) {
value, range, stop in
//Confirm the attribute value is actually a font
if let font = value as? UIFont {
//print(font)
//Check if the font is bold or not
if font.fontDescriptor.symbolicTraits.contains(.traitBold) {
//print("It's bold")
//Collect the range
boldRanges.append(range)
}
}
}
您可以通过正常方式更改这些范围内的颜色:
//Replace their colors.
let mutableAttributedText = attributedText.mutableCopy() as! NSMutableAttributedString
for boldRange in boldRanges {
mutableAttributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: boldRange)
}
descriptionTextView.attributedText = mutableAttributedText
基于上面的精彩回答,只是一个详细的代码片段来执行此操作。 x
是可变属性字符串。希望它可以节省一些打字时间。
let f = UIFont.systemFont(ofSize: 20)
let fb = UIFont.boldSystemFont(ofSize: 20)
let fi = UIFont.italicSystemFont(ofSize: 20)
let rangeAll = NSRange(location: 0, length: x.length)
var boldRanges: [NSRange] = []
var italicRanges: [NSRange] = []
x.beginEditing()
print("----------------------->")
x.enumerateAttribute(
NSFontAttributeName,
in: rangeAll,
options: .longestEffectiveRangeNotRequired)
{ value, range, stop in
if let font = value as? UIFont {
if font.fontDescriptor.symbolicTraits.contains(.traitBold) {
print("It's bold!")
boldRanges.append(range)
}
if font.fontDescriptor.symbolicTraits.contains(.traitItalic) {
print("It's italic!")
italicRanges.append(range)
}
}
}
x.setAttributes([NSFontAttributeName: f], range: rangeAll)
for r in boldRanges {
x.addAttribute(NSFontAttributeName, value: fb, range: r)
}
for r in italicRanges {
x.addAttribute(NSFontAttributeName, value: fi, range: r)
}
print("<-----------------------")
using.cachedAttributedString?.endEditing()
注意 - 此示例不处理令人讨厌的 同时 粗体和斜体的情况!我认为这样的信息更丰富。
我正在做一个项目,需要在 textview 中找到粗体字的范围并替换它们的颜色,我已经尝试了以下但没有成功。
.enumerateAttribute (NSFontAttributeName, in:NSMakeRange(0, descriptionTextView.attributedText.length), options:.longestEffectiveRangeNotRequired) { value, range, stop in
}
传递给 enumerateAttribute
和 NSFontAttributeName
闭包的 value
参数表示绑定到 range
的 UIFont
。因此,您只需要检查字体是否为粗体并收集范围即可。
//Find ranges of bold words.
let attributedText = descriptionTextView.attributedText!
var boldRanges: [NSRange] = []
attributedText.enumerateAttribute(NSFontAttributeName, in: NSRange(0..<attributedText.length), options: .longestEffectiveRangeNotRequired) {
value, range, stop in
//Confirm the attribute value is actually a font
if let font = value as? UIFont {
//print(font)
//Check if the font is bold or not
if font.fontDescriptor.symbolicTraits.contains(.traitBold) {
//print("It's bold")
//Collect the range
boldRanges.append(range)
}
}
}
您可以通过正常方式更改这些范围内的颜色:
//Replace their colors.
let mutableAttributedText = attributedText.mutableCopy() as! NSMutableAttributedString
for boldRange in boldRanges {
mutableAttributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: boldRange)
}
descriptionTextView.attributedText = mutableAttributedText
基于上面的精彩回答,只是一个详细的代码片段来执行此操作。 x
是可变属性字符串。希望它可以节省一些打字时间。
let f = UIFont.systemFont(ofSize: 20)
let fb = UIFont.boldSystemFont(ofSize: 20)
let fi = UIFont.italicSystemFont(ofSize: 20)
let rangeAll = NSRange(location: 0, length: x.length)
var boldRanges: [NSRange] = []
var italicRanges: [NSRange] = []
x.beginEditing()
print("----------------------->")
x.enumerateAttribute(
NSFontAttributeName,
in: rangeAll,
options: .longestEffectiveRangeNotRequired)
{ value, range, stop in
if let font = value as? UIFont {
if font.fontDescriptor.symbolicTraits.contains(.traitBold) {
print("It's bold!")
boldRanges.append(range)
}
if font.fontDescriptor.symbolicTraits.contains(.traitItalic) {
print("It's italic!")
italicRanges.append(range)
}
}
}
x.setAttributes([NSFontAttributeName: f], range: rangeAll)
for r in boldRanges {
x.addAttribute(NSFontAttributeName, value: fb, range: r)
}
for r in italicRanges {
x.addAttribute(NSFontAttributeName, value: fi, range: r)
}
print("<-----------------------")
using.cachedAttributedString?.endEditing()
注意 - 此示例不处理令人讨厌的 同时 粗体和斜体的情况!我认为这样的信息更丰富。