PDA

View Full Version : How to change the font size of text in UITextView dynamically in iPhone




city17
Nov 4, 2009, 11:21 AM
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.



PhoneyDeveloper
Nov 4, 2009, 12:18 PM
What does the debugger tell you? If you step through this code what happens?

city17
Nov 4, 2009, 12:21 PM
No it doesnt work. The size of the font is same as the currentsize which is 60 at the start.

dejo
Nov 4, 2009, 12:37 PM
No it doesnt work. The size of the font is same as the currentsize which is 60 at the start.
You haven't really answered PhoneyDeveloper's questions though.

Besides, this:
size = [self.txt_tipView.text sizeWithFont:[UIFont systemFontOfSize: currentSize] constrainedToSize:txtViewSize lineBreakMode:UILineBreakModeWordWrap];
self.txt_tipView.text is an NSString so you can't call sizeWithFont: on it.

You are getting warnings about this, aren't you?

city17
Nov 4, 2009, 01:08 PM
No I am not getting any warnings. Besides, sizeWithFont is a NSString function so why will it give warning.

dejo
Nov 4, 2009, 01:12 PM
Besides, sizeWithFont is a NSString function so why will it give warning.
Oops, my bad. Was looking at the basic NSString Class Reference.

PhoneyDeveloper
Nov 4, 2009, 01:26 PM
OK, you don't know how to use the debugger. You should learn.

What prints out if you put this line after the line that assigns size?

NSLog(@"size %@, currentSize %d, txtViewSize %@", NSStringFromCGSize(size), (int)currentSize, NSStringFromCGSize(txtViewSize));

city17
Nov 4, 2009, 02:59 PM
OK, you don't know how to use the debugger. You should learn.

What prints out if you put this line after the line that assigns size?

NSLog(@"size %@, currentSize %d, txtViewSize %@", NSStringFromCGSize(size), (int)currentSize, NSStringFromCGSize(txtViewSize));

Yes I do know the debugger, I just dint get you what exactly you wanted to know.
The values are as follows:

size = (108,70)
currentSize = 60
txtViewSize = (150,90)

Niiro13
Nov 4, 2009, 03:39 PM
You almost have it.

What you did was you constrained the size to the textview frame. Now if the size of the text is actually larger than that, then your return value is the constrained size. So "size" will always be 150, 150.

Now when you go:

if((size.width<=txtViewSize.width) && (size.height<=txtViewSize.height))

it will always return true, because the size will always be the size of the textview, so currentSize--; will never be called.

What you want to do is set constrainedSize to something like CGSizeMake(txtViewSize.width - 20, 10000).

Of course you'll still have to play around with it if you want to get rid of the white space at the end of the line, but that should take care of the following blank lines vertically.

PhoneyDeveloper
Nov 4, 2009, 04:12 PM
Niiri is right. The docs say

If the receiverís text does not completely fit in the specified size, it lays out as much of the text as possible and truncates it (for layout purposes only) according to the specified line break mode. It then returns the size of the resulting truncated string.

What that means is that the size returned will always be the same size or smaller than the constrained size, so you set the height of the constrained size to be large and the method figures out the size that the text needs in the specified width.

You might have been able to figure this out by stepping through the code and reading the docs.