Swift3 读取所有NSAttributedString属性及其有效范围
我正在进行一个项目,需要在textview中找到粗体字的范围并替换它们的颜色,我已经尝试了以下方法,但没有成功Swift3 读取所有NSAttributedString属性及其有效范围,swift3,nsattributedstring,Swift3,Nsattributedstring,我正在进行一个项目,需要在textview中找到粗体字的范围并替换它们的颜色,我已经尝试了以下方法,但没有成功 .enumerateAttribute (NSFontAttributeName, in:NSMakeRange(0, descriptionTextView.attributedText.length), options:.longestEffectiveRangeNotRequired) { value, range, stop in } 使用NSFontAttributeNa
.enumerateAttribute (NSFontAttributeName, in:NSMakeRange(0, descriptionTextView.attributedText.length), options:.longestEffectiveRangeNotRequired) { value, range, stop in
}
使用
NSFontAttributeName
传递给enumerateAttribute
闭包的value
参数表示绑定到范围的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)
}
}
}
基于上述惊人的答案,只需一个详细的代码片段就可以做到这一点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()
让f=UIFont.systemFont(大小:20)
让fb=UIFont.boldSystemFont(大小:20)
设fi=UIFont.italicSystemFont(大小:20)
让rangeAll=NSRange(位置:0,长度:x.length)
var boldRanges:[NSRange]=[]
var italicRanges:[NSRange]=[]
x、 开始编辑
打印(“-------------------------->”)
x、 枚举属性(
NSFontAttributeName,
在:兰格尔,
选项:.LongesEffectiveRangeNotRequired)
{值、范围、停止在
如果让font=值为?UIFont{
如果font.fontDescriptor.symbolicTraits.contains(.traitBold){
打印(“粗体!”)
boldRanges.append(范围)
}
如果font.fontDescriptor.symbolicTraits.contains(.tratitalic){
打印(“斜体!”)
斜体扩展。附加(范围)
}
}
}
x、 setAttributes([NSFontAttributeName:f],范围:rangeAll)
对于粗体范围内的r{
x、 addAttribute(NSFontAttributeName,值:fb,范围:r)
}
对于斜体中的r{
x、 addAttribute(NSFontAttributeName,值:fi,范围:r)
}
打印(“属性文本以外的任何其他方式?
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()