用计时器突出显示阿拉伯语文本
我正在尝试突出显示阿拉伯语文本,但它无法正常工作,最后该程序因错误而崩溃:
Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
我的代码是:
super.viewDidLoad() myMutableString = NSMutableAttributedString(string: longString) var currentLocation = 0 var currentLength = 0 var char = CharacterSet.whitespaces let arrayOfWords = longString.components(separatedBy: char) for word in arrayOfWords { currentLength = word.characters.count ranges.append(NSRange(location: currentLocation, length: currentLocation+currentLength)) currentLocation += currentLength + 1 } let mySelector = #selector(self.keepHighlighting) let timer = Timer.scheduledTimer(timeInterval:0.5, target: self, selector: mySelector, userInfo: tempLbl , repeats: true) timer.fire() }
并突出部分是:
func keepHighlighting(timer:Timer) { let lbl = timer.userInfo as! UILabel myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Al_Mushaf", size: 40.0)!, range:ranges[wordNum]) myMutableString.addAttribute(NSForegroundColorAttributeName,value: UIColor.blue,range: ranges[wordNum] ) lbl.attributedText = myMutableString wordNum = wordNum + 1 }
这段代码正在运行,但它们的索引范围最终未达到匹配…
看起来wordNum不断增加和增加。 如果你永远不会停止计时器或重置wordNum,这显然会最终在数组末尾运行。
您可能需要将计时器存储在属性中,并在您到达单词列表末尾时invalidate
。
删除timer.fire()
,计时器已在您创建时安排。 此外,尝试删除keepHighlighting
函数中的timer参数,并将userInfo
设置为nil
并直接从userInfo
调用标签。
- swift 3一起使用语音识别和AVFoundation
- 从底部加载导航视图控制器
- ITMS – 90179无效的代码签名/ ITMS – 90209无效的段alignment
- 如何检查HealthKit是否已被授权
- 当用户从Cordova的iOS通知中心select通知时更改状态
- Xcode UItesting – 用addSubview添加的Uiviews对于UItesting是完全不可见的
- 适用于Objective C开发人员的30个Swift概念-扩展
- UILabel UILongPressGestureRecognizer不工作?
- Swift:didSelectItemAtIndexpath函数不工作的JSON