I am making a highlighter app. It stores everything you've highlighted, and what file you've highlighted it from.
I have a problem, however. I know that all my syntax is correct and that the program should be working. I've tested everything I can think of to try and isolate the problem, and the best conclusion I could come to was that whenever I do
setAttributes_range_(styleshere,NSRange(?,?))it crashes because I am using the currently selected text for the start and ending index ranges in the NSRange object. I don't know why this affects it, but when I use predefined indexes and run the program, it does everything just fine.
How exactly are you getting the selected range?
ui.TextView().selected_rangethe attribute for my TextView.
A 2-tuple (start, end) that represents the current user selection. When setting this, a ValueError is thrown for ranges that are out of bounds.
Whereas NSrange takes a start, length.
attr_textis an attribute I set myself that stores the atrributed text of the TextView. Just a simpler way to find it when I need it.
And also where
dis a dictionary of attributes I have.
This doesn't work either. I tried to use the little bit of math in the second paramter of
NSRangeto grab the length of which I need.
the second param should just be selected_range-selected_range.
try creating the NSRange, and check the length is valid before you call the set attribute method. Perhaps you have an invalid selected range? (-1 for example)
Perhaps you could post a minimal example of something which works for hard coded values, but not for programattically generated values?
Also... you have a mutable attributed string, not a concrete one, right? and you have enabled editing?
Yes to the last couple questions. Let me look through it real quick. Thanks so much!
By the way, here is a simple example showing a similar problem, though using a regexp range instead of selected_text, but same basic idea.