How to change the font size of text in UITextView dynamically in iPhone
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 txtViewSi开发者_如何学编程ze = 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.
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.
精彩评论