所以我尝试动态设置蓝色背景颜色为UILabel
的归属文字,因为用户将手指沿着屏幕平移,但我不能为生活做好准备我弄清楚为什么我收到NSRangeException
时出现以下错误消息:Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds
。
这是我的代码:
var afterContext = ""
if textDocumentProxy.documentContextAfterInput != nil {
afterContext = textDocumentProxy.documentContextAfterInput!
} else {
afterContext = ""
}
if beforeContext != nil {
if initialContext == nil {
myRange = NSRange(location: 0, length:afterContext.characters.count)
let attrString = NSMutableAttributedString(string: selectedTextView.text!)
attrString.addAttribute(NSBackgroundColorAttributeName, value: UIColor(red: 20/255, green: 111/255, blue: 225/255, alpha: 1), range: myRange)
}
}
答案 0 :(得分:0)
这个简单的if条件好像已经修好了
if afterContext.characters.count > 0 {
myRange = NSRange(location: 0, length: afterContext.characters.count-1)
} else {
myRange = NSRange(location: 0, length: afterContext.characters.count)
}