Font size of text in textview changed back to default in the runtime.

Discussion in 'iOS Programming' started by jox0, Jan 10, 2014.

  1. jox0 macrumors newbie

    Joined:
    Jan 10, 2014
    #1
    What I want to do is to set font size in IB and only update text content in the runtime. For example, I put a textview in storyboard, then I set the font size to be 60. It was all good in the canvas, the size of the text is big. And in viewDidLoad method, I changed the text. Like this:

    Code:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *text = @"hello, world";
        self.textview.text = text;
    //    self.textview.font = [UIFont systemFontOfSize:60.0];
    }
    
    then the size of the text changed back to the default size. If I didn't comment out the last line, then the size will be set to 60. How can I fix this? Thanks.
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I had a similar issue in an OS X application just the other day. Can you assign an attributed string instead of a normal string? I would suggest using one of those, if it's possible in iOS (I haven't used attributed text in iOS before, but I would expect it to be similar to the OSX system.)
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Does UITextView under iOS 7 now adopt Dynamic Type? Could that be the cause?
     
  4. jox0, Jan 10, 2014
    Last edited: Jan 10, 2014

    jox0 thread starter macrumors newbie

    Joined:
    Jan 10, 2014
    #4
    thanks.

    I can assign an attributed string. It works. But I think using attributed text is same as reconfiguring the font size after assigned a plain text. Because If I did not set the NSFontAttributeName attribute, the result is same as before. It seems that the original font size information archived into storyboard file will disappear together with original text. If the text is changed, attributes like font size must be reset too. The code using attributed text:

    Code:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *text = @"hello, world";
        NSAttributedString *aText = [[NSAttributedString alloc]
                                     initWithString:text
                                     attributes:
                                     @{NSFontAttributeName: [UIFont systemFontOfSize:60.0]}];
        [self.textview setAttributedText:aText];
    //    self.textview.text = text;
    
    //    self.textview.font = [UIFont systemFontOfSize:60.0];
    }
    
    I still want to know if it is possible to set font size only once in IB.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    That's not correct. And easily proven: in code, change the text, then the font. Then change the text again. Has the font changed too? I doubt it.
     
  6. jox0 thread starter macrumors newbie

    Joined:
    Jan 10, 2014
    #6
    I don't know what is the reason. But I changed text again after I reconfigured the font size in the runtime, and again, font size changed back to the default size.
     

Share This Page