1

I am a newbie and I have a iOS project where I am setting the text property of a UILabel field.

self.nameLabel.text = @"abcd";

Question

  • Do I need to invoke setNeedsDisplayInRect for the label for the new text value to be displayed ?

My understanding based on some testing:

  • Without invoking setNeedsDisplayInRect, the the label's text was updated,
  • but I want to know if it was a coincidence or is gauranteed that the the label will display the new value without explicitly invoking setNeedsDisplay

Thanks

user1046037
  • 16,755
  • 12
  • 92
  • 138

2 Answers2

1

A UILabel will do everything needed for updates when you set its text property. Same for its other properties (font, textColor, etc.) About the only thing you have to do manually (if you're not using IB) is set the frame.

smparkes
  • 13,807
  • 4
  • 36
  • 61
  • Thanks a lot for the prompt answer, I have one doubt regarding what you said. Just to clarify if you set the frame in code, then you would need to invoke setNeedsDisplay ? – user1046037 Feb 23 '12 at 20:41
  • No. UIKit takes care of that. I only mentioned `frame` because a lot of people add `UILabel`s without setting the frame (`bounds` actually) and then wonder why it doesn't show up. Often/usually the right thing to do is `[label sizeToFit]`. – smparkes Feb 23 '12 at 20:44
0

If the label does not refresh despite the text update, your CPU may be fully utilized and the following command will force the refresh:

self.nameLabel.text = @"abcd";
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89