Crash in drawRect - what causes it?
* Terminating app due to uncaught exception 'NSRangeException', reason: '*
[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:]: Range or index out of bounds'
It is clearly a NSRange exception, as O' y said
I think you need add assert function to check the "range"
NSRange range = [val rangeValue];
make sure range will not be NSNotFound
.