1
self.name.font = [UIFont fontWithName:@"OpenSans" size:17.0];
self.name.textColor = [UIColor redColor];

'name' is of type UILabel. I am setting font and color of UILabel. I can see the color red as soon as I open the page (my viewcontroller). However, the font is initially of regular size 14 and takes some to update. If I stay on the page for sometime the font gets updated automatically to its new size 17.

Why is there a lag in updation of the 2 properties?

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41
A_G
  • 2,260
  • 3
  • 23
  • 56
  • You may have added static text in your storyboard file. Try removing that. – SNarula Dec 26 '16 at 11:22
  • Where you write this code? (in which method). and what you set for that label in your storyboard/xib file? – Shreyank Dec 26 '16 at 11:25
  • @SNarula I removed static text associated with UILabel 'name' from my .xib file. Still it takes time to upadte. – A_G Dec 26 '16 at 11:30
  • @Shreyank In .xib file there is some random text and font openSans-bold with size 14. And while initializing the cell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; I am setting the label's font and color. – A_G Dec 26 '16 at 11:31
  • Try to implement *layoutSubviews()* in UITableViewCell class. Just define `-(void)layoutSubviews()` and set the label font size inside it. – Poles Dec 26 '16 at 11:37

2 Answers2

2

Probably you are updating the ui elements outside main thread.

All ui elements should be updated in main thread. Not doing it could lead to inconsistent ui, delayed update and sometimes crashes as well.

If you are not in main thread and want to update ui elements. Check 2nd and 3rd answer(which is not accepted) in iOS - Ensure execution on main thread to know that.

Community
  • 1
  • 1
hridayesh
  • 1,123
  • 1
  • 14
  • 36
  • Put my comment inside dispatch_async(dispatch_get_main_queue(), ^{}); and worked perfectly! Thanks! – A_G Dec 26 '16 at 11:43
  • Actually replaced it with [[NSOperationQueue mainQueue] addOperationWithBlock:^ { //Your code goes in here NSLog(@"Main Thread Code"); }]; because apparently, there are some issues related with the previous approach as documented in the stack overflow post – A_G Dec 26 '16 at 11:47
  • No, there is nothing wrong with calling `dispatch_async(dispatch_get_main_queue(), ^{});` even if you are already on the main queue. – Rikh Dec 26 '16 at 12:59
  • I have not added the way of running code in main thread because it was already answered well in another link, so just included link! – hridayesh Dec 27 '16 at 06:20
0

Check if the font file exists and that the font name is same as @"OpenSans" , capitals also.

It may be because of name mismatch of font

You may check all the fonts in app by :

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    fontNames = [[NSArray alloc] initWithArray:
                 [UIFont fontNamesForFamilyName:
                  [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
    }
}
sashi_bhushan
  • 394
  • 3
  • 16