Why - [NSTextStorage replaceCharactersInRange: withAttributedString:] sometimes, sometimes not read fonts in other character sets?

I am trying to diagnose a problem in UKSyntaxColoredTextDocument 0.4 http://www.zathras.de/angelweb/blog-uksctd-oh-four.htm , where the text that actually lives in a different font than the one you specified will disappear when entering. (You can download and try this cool utility to see this problem for yourself ...)

Here is the background: this is some syntax code that is repainted as you type. It works fine, but if you enter some characters that are not part of the font set for this text view (e.g. Monaco, Helvetica) ... like a character symbol or something in Japanese that actually uses fonts, such as ZapfDingbatsITC or HiraKakuProN-W3 to display it, then these characters do not appear as you type.

Say you have this text: fdsafd [β˜€] sfds ‑ [β˜€β˜€β˜€] [ζ—₯本θͺž] ...

If you paste this into a text box and switch between the syntax coloring from the popup, it calls oldRecolorRange: with this line:

 [[textView textStorage] replaceCharactersInRange: range withAttributedString: vString]; 

Here, everything behaves as I expected. All ASCII text, characters, and Japanese text are visible. The value of [textView textStorage] starts and ends like this: (This is gdb output; it does not display Unicode characters, do not worry about it.)

  df {
     NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj = 0x001a4970, spc = 6.33";
 }? {
     NSFont = "ZapfDingbatsITC 20.00 pt. P [] (0x001ae720) fobj = 0x001bb370, spc = 5.56";
 } fdsafd [{
     NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj = 0x001a4970, spc = 6.33";
 }? {
     NSFont = "HiraKakuProN-W3 20.00 pt. P [] (0x001b59e0) fobj = 0x001bb600, spc = 6.66";
 }] sfds [{
 ...

... even after setting a new value

  dffdsafd [?] sfds [???] [???] Nihddfdfffdfdd {
     NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj = 0x001a4970, spc = 6.33";
 }

In other words, other people's fonts needed to display this line are saved automatically, although the fonts are not specified in the replacement line.

However, when you enter one character at a time, another call to replaceCharactersInRange:withAttributedString: in the recolorRange: method results in an assigned string that is only in the base font, no foreign character fonts are added for us, so characters from the range of the main font are not visible at all!

  dffdsafd [?] sfds [???] [???] Nihddfdfffdfddx {
     NSFont = "LucidaGrande 20.00 pt. P [] (0x001a3380) fobj = 0x001a4970, spc = 6.33";
 }

Any idea why this method will work in one way in one case and not in another? Is there some kind of switch that I can pass to give the NSTextStorage / NSAttributedString hint that we want the text to display foreign characters?

+4
source share
1 answer

Try [[textView textStorage] fixFontAttributeInRange: range]; after [[textView textStorage] replaceCharactersInRange: range withAttributedString: vString] in recolorRange:

I think the problem is to edit the text repository again in response to processEditing, which is already the tail of the edit. - [NSTextStorage processEditing] uses fixFontAttributeInRange :, but since you are trying to edit again, something goes wrong and the correction behavior is discarded.

When you do this for the entire document, there is a normal sequence beginEditing / endEditing / processEditing that calls the fixFontAttributeInRange: function to successfully call.

+4
source

All Articles