0

I have a number of UIButtons and I'm setting a custom font on them programmatically, like this:

Button1.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button2.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button3.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button4.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button5.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button6.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];

This code is correct and it works, but it doesn't seem like the leanest way of writing it. Is there a way to loop through these elements instead?

I had imagined something like this:

for (int i = 1; i <= 5; i++) {
    Button(i).titleLabel.font = etc.. // How would I get the value of 'i'?
}

Or is this simply a bad idea?

3
  • Whether the buttons are in an array? Or individual instances? Commented Feb 13, 2012 at 9:53
  • how often do you need to do this (in how many different places)? You could add them to an NSdictionary and the loop through to pull them back out & change properties, but it is hardly worth if to replace 6 lines of code. Commented Feb 13, 2012 at 9:54
  • It would only make sense to loop through this if the buttons are in a known order in some sort of collection Commented Feb 13, 2012 at 9:55

4 Answers 4

5

You may use an NSArray to iterate over:

UIFont *font = [UIFont fontWithName:@"myCustomFont" size:16];
NSArray *buttons = [NSArray arrayWithObjects: Button1, Button2, Button3, 
                                              Button4, Button5, Button6, 
                                              nil];
for (UIButton *button in buttons) {
  button.titleLabel.font = font;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's really neat. It's a shame I can only select one answer as correct! I'm choosing yours because it looks as though you were first.
Just remember not to forget the nil as last argument of [NSArray arrayWithObjects:] to avoid undefined behaviour. It's a so called variadic argument list, which need the nil to determine that there are no more arguments.
2

you can do like this:

NSMutableArray *buttons = [NSMutableArray array];
[buttons addObject: Button1];
[buttons addObject: Button2];
[buttons addObject: Button3];
[buttons addObject: Button4];
[buttons addObject: Button5];
[buttons addObject: Button6];

for (UIButton *button in buttons) {
    button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}

or

for (int i=1;i<7;i++) {
    SEL selector = selectorFromString([NSString stringWithFormat:@"Button%d", i]);
    UIButton *button = [self performSelector:selector];
    button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}

1 Comment

You can not add an object to NSArray as it is immutable (can not be modified after init). But you could use NSMutableArray to achieve that.
1

Well, I guess you can put all of your buttons in an array, then fast enumerate through it.


NSArray *buttons; 
//Put all of your buttons inhere
for (UIButton *button in buttons)
{
    button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}

Comments

0

How about this? currentView is the UIView that has all the buttons in it.

NSArray* buttonsArray = currentView.subviews;

for((UIButton*) button in buttonsArray)
{
     button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.