Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

city17

macrumors newbie
Original poster
Nov 4, 2009
4
0
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:

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.
 
No it doesnt work. The size of the font is same as the currentsize which is 60 at the start.
 
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:
Code:
size = [[B]self.txt_tipView.text[/B] 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?
 
No I am not getting any warnings. Besides, sizeWithFont is a NSString function so why will it give warning.
 
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?

Code:
NSLog(@"size %@, currentSize %d, txtViewSize %@", NSStringFromCGSize(size), (int)currentSize, NSStringFromCGSize(txtViewSize));
 
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?

Code:
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)
 
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:

Code:
 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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.