سؤال

I am implementing an IM app on iOS. I found that three20 library has a TTStyledTextLabel which provides cool features like showing images and url links. However I want to embed the TTStyledTextLabel in a message bubble (just like the sms app shipped with iphone does), where I need the label to adjust its size according to the text length. I found that TTStyledTextLabel can adjust its height according to its width, but I don't know how to make it shrink horizontally when the text is very short and can't fill up a whole line. Any suggestions?

هل كانت مفيدة؟

المحلول

I think I have a slightly better solution: I get the rootFrame of the ttstyledtext and iterate over its sibling frames to find the max width.

It works like this:

    TTStyledTextLabel* label = [[TTStyledTextLabel alloc] init];
    label.text = [TTStyledText textFromXHTML:myTextToBeDisplayed];
    [label sizeToFit];
    CGFloat maxWidth = 0;
    TTStyledFrame *f = label.text.rootFrame;
    while (f) {
        int w = f.x + f.width;
        if (w > maxWidth) {
            maxWidth = w;
        }
        f = f.nextFrame;
    }
    return CGSizeMake(maxWidth, label.height);

نصائح أخرى

I tried doing it by incrementally passing the width parameter in size to sizeToFit and looking at the resulting height to give cues in terms of whether the size is ok. But this is not a elegant solution

for (int index = 100; index < 320; index= index+30)

{ label.width = x; if (label.height < 20) break;

}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top