NSRange 异常 iOS
NSRange Exception iOS
这是我的代码:
我正在尝试格式化我的文本 NSMutableAttributedString(),但它似乎总是超出范围。
Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
extension ImageTableViewCell {
func formatLabel(code: SectionItem) {
print(code.statusType.title)
let stringFormatted = NSMutableAttributedString()
let range = (code.statusType.title as NSString).range(of: code.statusType.title)
print(range); stringFormatted.addAttribute(NSAttributedStringKey.foregroundColor, value: code.statusType.color, range:range)
stringFormatted.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: range)
self.titleLabel.attributedText = stringFormatted
}
}
我不知道可以修复
我试过:
NSRange
NSMakeRange(loc: 0, mytext.count)
请问还缺少什么?
您的范围问题是因为您的属性字符串为空。你从来没有给它初始文本。
变化:
let stringFormatted = NSMutableAttributedString()
至:
let stringFormatted = NSMutableAttributedString(string: code.statusType.title)
那么你的范围就可以了。当然,这是一种计算整个字符串范围的奇怪方法。简单地做:
let range = NSRange(location: 0, length: (code.statusType.title as NSString).length)
但是当属性应该应用于整个字符串时,有一种更简单的方法来创建属性字符串:
extension ImageTableViewCell {
func formatLabel(code: SectionItem) {
let attributes = [ NSAttributedStringKey.foregroundColor: code.statusType.color, NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue ]
let stringFormatted = NSAttributedString(string: code.statusType.title, attributes: attributes)
self.titleLabel.attributedText = stringFormatted
}
}
这是我的代码: 我正在尝试格式化我的文本 NSMutableAttributedString(),但它似乎总是超出范围。
Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
extension ImageTableViewCell {
func formatLabel(code: SectionItem) {
print(code.statusType.title)
let stringFormatted = NSMutableAttributedString()
let range = (code.statusType.title as NSString).range(of: code.statusType.title)
print(range); stringFormatted.addAttribute(NSAttributedStringKey.foregroundColor, value: code.statusType.color, range:range)
stringFormatted.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: range)
self.titleLabel.attributedText = stringFormatted
}
}
我不知道可以修复
我试过:
NSRange
NSMakeRange(loc: 0, mytext.count)
请问还缺少什么?
您的范围问题是因为您的属性字符串为空。你从来没有给它初始文本。
变化:
let stringFormatted = NSMutableAttributedString()
至:
let stringFormatted = NSMutableAttributedString(string: code.statusType.title)
那么你的范围就可以了。当然,这是一种计算整个字符串范围的奇怪方法。简单地做:
let range = NSRange(location: 0, length: (code.statusType.title as NSString).length)
但是当属性应该应用于整个字符串时,有一种更简单的方法来创建属性字符串:
extension ImageTableViewCell {
func formatLabel(code: SectionItem) {
let attributes = [ NSAttributedStringKey.foregroundColor: code.statusType.color, NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue ]
let stringFormatted = NSAttributedString(string: code.statusType.title, attributes: attributes)
self.titleLabel.attributedText = stringFormatted
}
}