"Orphaned" words not reported by NSString -boundingRectWithSize...

  • I've made a window where messages ( just strings ) stack from top to
    bottom. The window is fixed width, so if a message is too long it
    word wraps. Finally, since I show several messages, I have to keep
    track of how tall each is so I can lay it out correctly.

    Now, 99% of the time this works. But when the text is just long
    enough that a single word wraps to the next line, the reported height
    for the string doesn't show that line, just the lines up to it. I can
    better show this with code:

    First, here's how I set up the string attributes:

    NSMutableParagraphStyle *pstyle = [[NSMutableParagraphStyle alloc]
    init];
    [pstyle setParagraphStyle: [NSParagraphStyle defaultParagraphStyle]];
    [pstyle setAlignment: NSCenterTextAlignment];
    [pstyle setLineBreakMode: NSLineBreakByWordWrapping];

    _textDisplayAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
        [NSFont messageFontOfSize: [NSFont smallSystemFontSize]],
    NSFontAttributeName,
        [NSColor whiteColor], NSForegroundColorAttributeName,
        pstyle, NSParagraphStyleAttributeName,
        nil] retain];

    And here's the method preferredHeight which returns the height needed
    for the string to display properly:

    - (float) preferredHeight
    {
        NSRect br = [_message boundingRectWithSize: NSMakeSize( _width,
    0 ) options:NSStringDrawingUsesLineFragmentOrigin attributes:
    _textDisplayAttributes];
        return br.size.height + (2.0 * _vPad);
    }

    I have no idea why an orphaned word wouldn't be reported. Am I doing
    something bone-headed here?

    Thanks,

    <shamyl...>
        "obviously, you're not a golfer"
            -- the Dude
  • The code shown should do exactly what you're trying to do, and I
    cannot reproduce it with a simple sample project.
    So, I'm not sure.

    On thing to note if you're rendering the text using NSCell.

    NSCell as paddings on both sides of text when center-aligned.

    If that's the case, you might be measuring with width slightly wider
    than the one used at the rendering time.

    Aki

    > I've made a window where messages ( just strings ) stack from top to
    > bottom. The window is fixed width, so if a message is too long it
    > word wraps. Finally, since I show several messages, I have to keep
    > track of how tall each is so I can lay it out correctly.
    >
    > Now, 99% of the time this works. But when the text is just long
    > enough that a single word wraps to the next line, the reported
    > height for the string doesn't show that line, just the lines up to
    > it. I can better show this with code:
    >
    > First, here's how I set up the string attributes:
    >
    > NSMutableParagraphStyle *pstyle = [[NSMutableParagraphStyle alloc]
    > init];
    > [pstyle setParagraphStyle: [NSParagraphStyle defaultParagraphStyle]];
    > [pstyle setAlignment: NSCenterTextAlignment];
    > [pstyle setLineBreakMode: NSLineBreakByWordWrapping];
    >
    > _textDisplayAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
    > [NSFont messageFontOfSize: [NSFont smallSystemFontSize]],
    > NSFontAttributeName,
    > [NSColor whiteColor], NSForegroundColorAttributeName,
    > pstyle, NSParagraphStyleAttributeName,
    > nil] retain];
    >
    >
    > And here's the method preferredHeight which returns the height
    > needed for the string to display properly:
    >
    > - (float) preferredHeight
    > {
    > NSRect br = [_message boundingRectWithSize: NSMakeSize( _width,
    > 0 ) options:NSStringDrawingUsesLineFragmentOrigin attributes:
    > _textDisplayAttributes];
    > return br.size.height + (2.0 * _vPad);
    > }
    >
    > I have no idea why an orphaned word wouldn't be reported. Am I doing
    > something bone-headed here?
    >
    > Thanks,
    >
    > <shamyl...>
    > "obviously, you're not a golfer"
    > -- the Dude
  • That is *exactly* what it was, and I'm a little embarrassed. Thanks
    for the clue-stick!

    <shamyl...>
        lorem ipsum dolor sit amet,

    On May 25, 2007, at 2:18 PM, Aki Inoue wrote:

    > The code shown should do exactly what you're trying to do, and I
    > cannot reproduce it with a simple sample project.
    > So, I'm not sure.
    >
    > On thing to note if you're rendering the text using NSCell.
    >
    > NSCell as paddings on both sides of text when center-aligned.
    >
    > If that's the case, you might be measuring with width slightly
    > wider than the one used at the rendering time.
    >
    > Aki
    >
    >> I've made a window where messages ( just strings ) stack from top
    >> to bottom. The window is fixed width, so if a message is too long
    >> it word wraps. Finally, since I show several messages, I have to
    >> keep track of how tall each is so I can lay it out correctly.
    >>
    >> Now, 99% of the time this works. But when the text is just long
    >> enough that a single word wraps to the next line, the reported
    >> height for the string doesn't show that line, just the lines up to
    >> it. I can better show this with code:
    >>
    >> First, here's how I set up the string attributes:
    >>
    >> NSMutableParagraphStyle *pstyle = [[NSMutableParagraphStyle alloc]
    >> init];
    >> [pstyle setParagraphStyle: [NSParagraphStyle defaultParagraphStyle]];
    >> [pstyle setAlignment: NSCenterTextAlignment];
    >> [pstyle setLineBreakMode: NSLineBreakByWordWrapping];
    >>
    >> _textDisplayAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
    >> [NSFont messageFontOfSize: [NSFont smallSystemFontSize]],
    >> NSFontAttributeName,
    >> [NSColor whiteColor], NSForegroundColorAttributeName,
    >> pstyle, NSParagraphStyleAttributeName,
    >> nil] retain];
    >>
    >>
    >> And here's the method preferredHeight which returns the height
    >> needed for the string to display properly:
    >>
    >> - (float) preferredHeight
    >> {
    >> NSRect br = [_message boundingRectWithSize: NSMakeSize( _width,
    >> 0 ) options:NSStringDrawingUsesLineFragmentOrigin attributes:
    >> _textDisplayAttributes];
    >> return br.size.height + (2.0 * _vPad);
    >> }
    >>
    >> I have no idea why an orphaned word wouldn't be reported. Am I
    >> doing something bone-headed here?
    >>
    >> Thanks,
    >>
    >> <shamyl...>
    >> "obviously, you're not a golfer"
    >> -- the Dude
    >
previous month may 2007 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