I work with NSTextView, and one of the requirements that I have is that the tab character '\ t' should have the same width as four spaces.
So, the text content will look like this:
AAAA AAAA - 1 tab AAAA - 4 spaces
And here is how I do it:
// done when NSTextView first loaded and when // delegate textDidBeginEditing gets called: (perhaps overkill, but is a work in progress). - (void)updateMyTextViewTextAttributes { NSMutableParagraphStyle* paragraphStyle = [[myTextView defaultParagraphStyle] mutableCopy]; if (paragraphStyle == nil) { paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; } float charWidth = [[myFont screenFontWithRenderingMode:NSFontDefaultRenderingMode] advancementForGlyph:(NSGlyph) ' '].width; [paragraphStyle setDefaultTabInterval:(charWidth * 4)]; [paragraphStyle setTabStops:[NSArray array]]; [myTextView setDefaultParagraphStyle:paragraphStyle]; NSMutableDictionary* typingAttributes = [[myTextView typingAttributes] mutableCopy]; [typingAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; [typingAttributes setObject:scriptFont forKey:NSFontAttributeName]; [myTextView setTypingAttributes:typingAttributes]; }
This allows you to display the corresponding layout with the source text, as well as keep the input attributes the same.
The problem is that the end user can change the font. And when that happens, the sample text will be biased. Very similar to the following:
[smaller font] AAAA AAAA - 1 tab AAAA - 4 spaces
[larger font] AAAA AAAA - 1 tab AAAA - 4 spaces
I tried calling myTextView setNeedsDisplay: YES when I read that it was calling NSTextView setNeedsDisplayInRect: avoidAdditionalLayout with NO for the avoidAdditionalLayout parameter. It didn’t change anything.
I tried calling my call to updateMyTextViewTextAttributes when myTextView has a new set of myFont. This does not change anything.
I also tried passing the layoutManager myTextView to provide a LayoutForTextContainer for the textContainer myTextView. Without changes.
At this moment, I'm not sure what to do next. Any suggestions?
objective-c fonts cocoa appkit
Lyndsey ferguson
source share