NSTextView's -setTextContainerInset: issues

  • Hello,

    I have a text view that can be scaled by the user. In addition, the text view has "margins" around it that I create using -setTextContainerInset:. When I change the scaling, I update the text container inset to remain in proportion (so that it always looks about 20 pixels all around no matter what the scale is). However, it seems that scaling in conjunction with setting thet text container inset can create some weird redrawing bugs. When the text is scaled and there is a text container inset, if the user scrolls the text, there will be white horizontal lines through some of the lines of text, as though the visible area has not been redrawn correctly. This only happens when the user scrolls, the text is scaled, and there is an inset. If I set the text container inset to 0, everything works fine.

    Investigating this, I was interested in this note in the docs under -setTextContainerInset:

    Discussion
    It is possible to set the text container and view sizes and resizing behavior so that the inset cannot be maintained exactly, although the text system tries to maintain the inset wherever possible. In any case, the textContainerOrigin and size of the text container are authoritative as to the location of the text container within the view.

    Does this explain the behaviour I am seeing? Or is this a display bug in NSTextView? Does anybody know if there is anything I can do to work around this? The code I am using to scale the text view is included below.

    Many thanks in advance,
    Keith

    Here is the code I use to scale the text view:

    - (void)rescaleTextViewWithNewScale:(int)scalePercent

    {

        if (scalePercent == 0)

            return;



        float zoom = (float)scalePercent / 100.0;



        NSRect textViewBounds = [textView frame];

        textViewBounds.size.height = textViewBounds.size.height / zoom;

        textViewBounds.size.width = textViewBounds.size.width / zoom;

        [textView setBounds:textViewBounds];



        // Ensure offset always looks about 20 pixels rather than getting bigger or smaller depending on scale

        float scale = [textView frame].size.height/[textView bounds].size.height;

        float insetWidth = (int)(20.0/scale);

        float insetHeight = (int)(20.0/scale);

        [textView setTextContainerInset:NSMakeSize(insetWidth,insetHeight)];





        [textView sizeToFit];



        [textView setFrameSize:[textView frame].size];



        [textView setNeedsDisplayInRect:[textView visibleRect]];

    }
  • On Oct 22, 2006, at 4:37 AM, Keith Blount wrote:

    > It is possible to set the text container and view sizes and
    > resizing behavior so that the inset cannot be maintained exactly,
    > although the text system tries to maintain the inset wherever
    > possible. In any case, the textContainerOrigin and size of the text
    > container are authoritative as to the location of the text
    > container within the view.
    >
    > Does this explain the behaviour I am seeing? Or is this a display
    > bug in NSTextView? Does anybody know if there is anything I can do
    > to work around this? The code I am using to scale the text view is
    > included below.

    What you're seeing sounds like a display bug, although I have not
    seen this exact issue.  The issues that are being noted in the
    documentation have to do with setting the origin when there is not
    enough space in the text view for the entire container.

    I notice that you are setting the inset to integral values in
    container and view coordinates, but when the view is scaled that does
    not correspond to integral values in screen space.  You might try
    experimenting with that.

    Douglas Davidson
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today