0

I have this in my property :

@property (strong, nonatomic) IBOutlet UILabel *titleLogin;
@property (strong, nonatomic) IBOutlet UILabel *titleRegister;
@property (strong, nonatomic) IBOutlet UILabel *labelTerms;
@property (strong, nonatomic) IBOutlet UILabel *titleVote;
@property (strong, nonatomic) IBOutlet UILabel *labelVote;

on the other hand, I have this loop :

for (UIView *subview in [[self view] subviews]) {
    if ([subview isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)subview;
        [textField setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
    } else if ([subview isKindOfClass:[UILabel class]]){
        UILabel *label = (UILabel *)subview;
        [label setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];
    }
}

with that loop, all of my UILabel has same font size, but I want to make exception for *labelTerms, *labelVote and some of other label in the future development.

how to make this exception in loop or in my property? thanks in advance...

Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • This seems a lot of code and complexity to avoid a small number of clear and simple `setFont:` calls. You can assign this configuration directly in IB as well. Is the actual problem much larger than the code you've presented here? – Rob Napier Sep 11 '13 at 16:41
  • nope. that's only what I need to know... and you mean I can assign custom Font directly from StoryBoard? how? – Saint Robson Sep 11 '13 at 16:45
  • 2
    In your storyboard, select the label. Open the property inspector and change the font. Tadaaa. – Rob van der Veer Sep 11 '13 at 17:02
  • I found this : http://stackoverflow.com/questions/9090745/custom-font-in-a-storyboard/15155081#15155081 Tadaaaa.... ;-) – Saint Robson Sep 11 '13 at 17:05

2 Answers2

1

Try

if(subView != labelTerms)
{
   .. etc...

I assume you are smart enough to figure out the rest.

Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20
  • unfortunately, I'm a PHP developer who try to enter Objective-C world and this is my 4th day with it... I really appreciate if you could give me some clue for it... thanks.. – Saint Robson Sep 11 '13 at 16:42
  • `else if (subView != labelTerms && [subview isKindOfClass:[UILabel class]]){` – Joe Sep 11 '13 at 16:55
  • Also, I suggest that you check out a crash course on Objective C first. Check out this Cheat Sheet http://cdn5.raywenderlich.com/downloads/RW-Objective-C-Cheatsheet-v1_2.pdf – Rob van der Veer Sep 11 '13 at 17:05
1

Set the tag of each subview in your xib/storyboard file. Make sure that every subview that you want to have the "ABeeZee-Regular" font has 1 as tag, and 2 for the subviews that you want to have the "Raleway-ExtraLight" font:

enter image description here

Make sure that there isn't a subview with tag 1 or 2 that isn't a label/text field, then the code may be simplified as this:

for (id subview in [[self view] subviews]) {
    if ([subview tag]==1) {
        [subview setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
    } else if ([subview tag]==2){
        [subview setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];
    }
}

Or better:

id fonts[]= { [UIFont fontWithName:@"ABeeZee-Regular" size:14], [UIFont fontWithName:@"Raleway-ExtraLight" size:28]};
for (id subview in [[self view] subviews]) {
    if([subview tag])
        [subview setFont: fonts[[subview tag]-1] ];
}

PS: If you have many subviews and you want to group them, also (re) consider the idea of using an array for every group of subviews that you want to have the same properties.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • I like your solution, but I have this error message in return : Property 'tag' not found on object of type 'const __strong id', how to fix this? thank you... – Saint Robson Sep 12 '13 at 03:08
  • 1
    @RobertHanson Since subview has type id, the compiler can't know if it has a tag property. You can fix this error by calling [subview tag] instead of subview.tag, I edited the answer and corrected the error. – Ramy Al Zuhouri Sep 12 '13 at 07:14