iphonethree20ttstyledtextlabel

How to adjust width of TTStyledTextLabel?


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?


Solution

  • 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);