My layout got messed up.

Imagine that the "AAAA - 1 tab" is is tabbed in with the width of four
spaces and that the "AAAA - 4 spaces" block lines up along with it.

Also, imagine that:

[smaller font]
AAAA
AAAA - 1 tab
AAAA - 4 spaces

has the "AAAA - 1 tab" offset to the right of the "AAAA - 4 spaces" block,
and:

[larger font]
AAAA
AAAA - 1 tab
AAAA - 4 spaces

has the "AAAA - 1 tab" offset to the left of the "AAAA - 4 spaces" block.

Thanks,
Lyndsey

On Fri, Feb 19, 2010 at 12:03 PM, <lyndsey.fergu...@gmail.com> wrote:

> I'm working with an NSTextView and one of the requirements I have is that a
> tab character, '\t', shall have the same width as four spaces.
>
> So the text-content would look like this:
>
> AAAA
> AAAA - 1 tab
> AAAA - 4 spaces
> And this is how I accomplish this:
>
> // done when NSTextView first loaded and when
> // delegate's 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 the appropriate layout to be shown with the initial text as
> well as keep the typing attributes the same.
>
> The problem is that the end-user can change the font. And when that
> happens, the sample text becomes misaligned. Much like the below:
>
> [smaller font]
> AAAA
> AAAA - 1 tab
> AAAA - 4 spaces
>
>
> [larger font]
> AAAA
> AAAA - 1 tab
> AAAA - 4 spaces
> I've tried calling myTextView's setNeedsDisplay:YES as I read that it ends
> up calling NSTextView's setNeedsDisplayInRect:avoidAdditionalLayout with a
> NO for the avoidAdditionalLayout parameter. This didn't change anything.
>
> I've tried calling my updateMyTextViewTextAttributes call when myTextView
> has the new myFont set. That doesn't change a thing.
>
> I've also tried telling the layoutManager of myTextView to
> ensureLayoutFOrTextContainer for the textContainer of myTextView. No change.
>
> At this point, I'm not sure what to try next. Any suggestions?
>
> Thanks in advance,
> Lyndsey Ferguson
>
> P.S. I've posted this to stackoverflow.com as well:
>
>
> http://stackoverflow.com/questions/2284150/how-to-relayout-content-of-nstextview-so-that-my-tab-characters-are-drawn-with-wi




-- 
Lyndsey Ferguson
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to