I 'm trying to add attributes to a certain range of characters in textView on basis of break line character.The app is crashing and throwing out of bound error.Not sure why because the range to which I'm applying attributes is within the text length.
Here's the method to get the range:
func findParagraphBy(postion:Int)->NSRange{
var startIndex = 0
var endIndex = 0
for (index,char) in textView.text.enumerated(){
if index<postion && char == "\n"{
startIndex = index
}else if index>=postion && char == "\n" && endIndex == 0{
endIndex = index
}else{
continue
}
}
if endIndex == 0{
endIndex = self.textView.text.count-1
}
let range = NSMakeRange(startIndex, endIndex)
return range
}
Here's the method to apply Attributes
func applyAttr(){
let range = findParagraphBy(postion: textView.selectedRange.location-1)
self.textView.textStorage.addAttributes(self.currentAttribute.getAttr(), range: range)
}