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

Discussion in 'iOS Programming' started by city17, Nov 4, 2009.

  1. macrumors newbie

    Joined:
    Nov 4, 2009
    #1
    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.
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    What does the debugger tell you? If you step through this code what happens?
     
  3. thread starter macrumors newbie

    Joined:
    Nov 4, 2009
    #3
    No it doesnt work. The size of the font is same as the currentsize which is 60 at the start.
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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?
     
  5. thread starter macrumors newbie

    Joined:
    Nov 4, 2009
    #5
    No I am not getting any warnings. Besides, sizeWithFont is a NSString function so why will it give warning.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Oops, my bad. Was looking at the basic NSString Class Reference.
     
  7. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    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));
     
  8. thread starter macrumors newbie

    Joined:
    Nov 4, 2009
    #8
    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)
     
  9. macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #9
    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.
     
  10. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    Niiri is right. The docs say

    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.
     

Share This Page