tags:

views:

49

answers:

2

I have a UITextView that is supposed to have one line of text.

The size font is controlled by a slider. As the user controls the slider, I calculate the new UITextView size using (this is the method called by the slider as it moves).

- (void) changeFontSize:(id)sender {
    UISlider *slider = (UISlider *)sender;
    CGFloat newSize = (CGFloat)[slider value];

    myTextView.font = [UIFont boldSystemFontOfSize:newSize];
    [self adjustBoxforSize:myTextView.text];
}

- (void) adjustBoxForSize:(NSString*)myText {

    CGFloat fontSize = myTextView.font.pointSize;

    CGSize newSize = 
        [myText sizeWithFont:[UIFont boldSystemFontOfSize:fontSize]];
    newSize.width = newSize.width * 1.5; // make it 50% larger 
    newSize.height = newSize.height * 1.5; // make it 50% taller
    CGRect myFrame = CGRectMake(0.0f, 0.0f, newSize.width, newSize.height);
    myTextView.frame = myFrame;
}

If I reduce the font, this is the result

alt text

The question is: how can the text be out of the textview if I am scaling it to fit and indeed making it 50% larger??

am I missing something? It seems that sizeWithFont is not calculating the size correctly.

Thanks.

+1  A: 

Maybe I'm wrong, but this:

newSize.width = fontSize.width * 1.5; // make it 50% larger 
newSize.height = fontSize.height * 1.5; // make it 50% taller

Shouldn't be like this?

newSize.width = newSize.width * 1.5; // make it 50% larger 
newSize.height = newSize.height * 1.5; // make it 50% taller

Otherwise you are using font size instead of the computed text size.

MaxFish
sorry about that. It was a typo when I copied the code from my app to here (I had to simplify it and got it wrong). I've corrected the question.
Digital Robot
Try this: newSize.width = newSize.width + 20; It seems paddings don't grow with font size.
MaxFish
this seems to solve one problem. Doing it by percentage is not good, because you will have small increases when the font is small, but doing it using a fixed amount will do the trick. Please repeat your comment as an answer, so I can accept. thanks!
Digital Robot
+1  A: 

TextView has paddings that don't grow with the view size. Adding a fixed amount to the computed size of the text do the trick. Something like this:

newSize.width = newSize.width + 20; 

20 seems a good number for you case, but you can try tweaking it.

MaxFish