How to change font size of text in UITextView dynamically in iPhone

I have a fixed size UITextView says width and height 150 150.

The goal is to reflect the thought of the day. Please note that the size must remain constant and I cannot change it.

Now the length of the thought line depends on the thought. I want to change the font size of the text to make sure it doesn't show white space in the UITextView if the length is small, or doesn't show scrolling if it's larger.

So how do I change the font of the UITextView according to the line length of the thought.

What's wrong with the following code:

CGSize size;
    BOOL run=TRUE;
    CGSize txtViewSize = self.txt_tipView.frame.size;
    while(run){

        size = [self.txt_tipView.text sizeWithFont:[UIFont systemFontOfSize: currentSize] constrainedToSize:txtViewSize lineBreakMode:UILineBreakModeWordWrap];
        if((size.width<=txtViewSize.width) && (size.height<=txtViewSize.height))
            run = FALSE;
        else
            currentSize--;
    }
    self.txt_tipView.font = [UIFont fontWithName:@"Helvetica" size:currentSize];//[UIFont boldSystemFontOfSize:12];

      

What happens is the size of the text in the TextView is always 60. There is only one word per line.

+2


source to share


1 answer


Set an implicit font size, say the largest acceptable font that you could use. Then take a measurement of the text size with:

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize: currentSize] constrainedToSize: maxTextSize lineBreakMode:UILineBreakModeWordWrap];

      



If the size you receive is out of bounds, adjust the font size and repeat. MaxTextSize should be the size of your UITextView.

+4


source







All Articles