tags:

views:

4208

answers:

2

Hello all,

I have a fixed size UITextView say width and height of 150,150.

The purpose is to display the thought of the day. Please note the size need to remain constant and I cant change it.

Now The length of the thought string varies with respect to thought. What I want to do is change the size of font of the text to make sure it dont show any empty space in UITextView if length is small or it dont show the scroll if its bigger.

So how to vary the font of UITextView according to the length of thought string.

What is 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 TextView is always 60. That is each line has only one word.

+2  A: 

See UITextField's adjustsFontSizeToWidth property.

Darren
This question is referring to UITextView and not UITextField.
bentford
+3  A: 

Set an implicit font size, let's say the largest acceptable font you could use. Then, make a measurement of your text size with:

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

If the size you obtain is off the acceptable bounds, adjust the font size and repeat. The maxTextSize should be the size of your UITextView.

luvieere
can you add some more elaboration to the code. Like how exactly I need to do it. I am doing it following way 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) else currentSize--; } self.txt_tipView.font = [UIFont fontWithName:@"Helvetica" size:currentSize];
rkb
Looks ok to me. Any problems with it?
luvieere
If it doesn't work out with a UITextView, perhaps you should test it with a UILabel... that is if the only function you expect out of it is *displaying* the thought of the day.
luvieere
Yeah it dont correct the size instead it ends up with with the font size of 60 showing one word in each line.
rkb
Is your whole text displayed, or does it get truncated? Do all the words show up one on each line, with font 60, or just a few of them?
luvieere
No whole text is displayed and the text gets fits in the UITextView so its like one word covering the whole textView and you need to scroll to see all the lines.
rkb
I see... what do self.txt_tipView.frame.size.width and self.txt_tipView.frame.size,height output? NsLog them and check they are indeed 150... Try checking if the strange behavior persists if you replace the UITextView with an UILabel.
luvieere
No height and width are according to the size of my UITextView that is width= 160 and height = 90. But even I use UILabel How will it solve the problem of multiline, like the the number of lines will vary according to the length of string.
rkb
UILabel has a *numberOfLines* property, that if you set to 0 will wrap as many lines as it takes to display the text.
luvieere
Will you be able to help me with the code if it is possible for you to replace the UITextView in my code in question to UILabel.
rkb
Like I am getting little bit confused when I tried it. Tnx for all ur help.
rkb