When there are multiple TextFormatRanges in a TextField, there appears to be an indexing issue. The end index of the first format will equal the beginning format of the second range. For example, if I have the word “three” with the “th” set as one TextFormat and “ree” as a second, the indexes will be as follows:
Somehow the TextField parses this and displays it correctly, the problem I’m encountering is when I call TextField.getTextFormat(2,2);
I want to know what the correct TextFormat is for the character at index 2 (the letter “r”). I would expect to get the format information from the second TextFormat range (that encompasses “ree”), however getTextFormat thinks BOTH ranges apply to it, since the end index of the first range is 2, which is the same as the start index of the second range. getTextFormat resolves this conflict by nulling out conflicting values in the return TextFormat.
I’m not sure what the correct solution here is–I would expect range one (the “th”) to have a start index of 0 and an end index of 1 (instead of 2), and range two (the “ree”) to have a start index of 2 and end of 4. So it seems like modifying TextField.setTextFormat makes the most sense–to decrement the end index by 1. However I’m not all that familiar with the underlying TextEngine code so I’m not sure if there’s a valid reason to not do this.
I have a workaround that modifies TextField.getTextFormat, where I modify the range check from this:
if ((group.start <= beginIndex && group.end >= beginIndex) || (group.start <= endIndex && group.end >= endIndex))
if ((group.start <= beginIndex && group.end > beginIndex) || (group.start <= endIndex && group.end > endIndex))
I changed the “group.end” checks from >= to >. This solves my immediate problem, but I’m not sure if the indexes are supposed to be overlapping to begin with or not.