Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing UILabel font

  • Say I have three UILabels.
  • I create a font and assign that font to all three labels.

Now I edit this font's type/size etc and expect the fonts of those labels to change too, which does not happen. I basically need to apply a single font to multiple labels and I know this can be done with an outlet collection but i'd just like some help in understanding the flaw in my logic here so here's my code...

self.labelFont = [UIFont fontWithName:@"System" size:12];

self.label1.font = self.labelFont;
self.label2.font = self.labelFont;
self.label3.font = self.labelFont;

self.labelFont = [UIFont fontWithName:@"Courier" size:30];

Thank

like image 823
silence_is_eazy Avatar asked Sep 19 '13 10:09

silence_is_eazy


1 Answers

In your code you create two objects, [UIFont fontWithName:@"System" size:12], and [UIFont fontWithName:@"Courier" size:30].

Initially, your labelFont variable points to the first font object. Then you copy that pointer in the assignments to fonts of the three labels. At this moment, you have four pointers referencing the same object - namely, the object returned by the [UIFont fontWithName:@"System" size:12] call.

Illustration 1

Next, you change labelFont to point to [UIFont fontWithName:@"Courier" size:30]. Once you do that, the labelFont starts pointing to that new object; it no longer points to the old object. However, the three labels are still pointing to the old font, so their appearance does not change.

Illustration 2

like image 106
Sergey Kalinichenko Avatar answered Sep 30 '22 07:09

Sergey Kalinichenko