Highlight Arabic Text with Timer

大憨熊 提交于 2020-01-05 08:44:12

问题


I'm trying to highlight the arabic text but it is not working,at last the program crashed with error:

Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'

my code is :

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()

}

and highlight part is:

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

    }

This code is working but their index range miss matched at last...


回答1:


It looks like wordNum keep increasing and increasing without bound. If you never stop the timer or reset wordNum, this is clearly going to run off the end of the array eventually.

You likely need to store the timer in a property and invalidate it whenever you get to the end of your word list.




回答2:


Delete the timer.fire(), the timer is already scheduled when you create it. Also, try to delete the timer parameter in the keepHighlighting function and set the userInfo to nil and call the label from the userInfo directly.



来源:https://stackoverflow.com/questions/43393458/highlight-arabic-text-with-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!