views:

510

answers:

1

Hi All:

I have a Uitextview that is populated with a text file. I have a control on the page that allows the user to enable and disable paging. I am having a problem where the top and/or bottom line of the text is sometimes "split in half" so you can only see the top or bottom half of the line. I believe the code below should fix it. The code gets the line height of the text and the frame height, figures out the number of lines that are visible on the screen, and creates a new frame height so it will fit. While the frame does get resized, it still "cuts off" the top and/or bottom line. Anyone one have any suggestions? Is my math wrong?

Thanks!!!

 - (void)fitText
    {

            CGFloat maximumLabelHeight = 338;
            CGFloat minimumLabelHeight = 280;

            CGSize lineSize = [theUiTextView.text sizeWithFont:theUiTextView.font];
            CGFloat lineSizeHeight = lineSize.height;
            CGFloat theNumberOfLinesThatShow = theUiTextView.frame.size.height/lineSize.height;

            //adjust the label the the new height
            theNumberOfLinesThatShow = round(theNumberOfLinesThatShow);

            CGFloat theNewHeight = lineSizeHeight * theNumberOfLinesThatShow;

            if (theNewHeight > maximumLabelHeight)
        {
         theNumberOfLinesThatShow = theNumberOfLinesThatShow - 1;
         theNewHeight = lineSizeHeight * theNumberOfLinesThatShow;
        }
        if (theNewHeight < minimumLabelHeight)
        {
         theNumberOfLinesThatShow = theNumberOfLinesThatShow + 1;
         theNewHeight = lineSizeHeight * theNumberOfLinesThatShow;
        }

            //adjust the label the the new height.
            CGRect newFrame = theUiTextView.frame;
            newFrame.size.height = theNewHeight;
            theUiTextView.frame = newFrame;
}
A: 

UIScrollView (and UITextView is a UIScrollView) seems to use a fixed 8-pixel inset on both sides. This seems to be independent of alignment or font size.

So, I think what you're missing here is the fudge factor of 16.0 - see this question.

Jane Sales