我有一些UIButtons,我正在以编程方式设置自定义字体,如下所示:
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];
Run Code Online (Sandbox Code Playgroud)
这段代码是正确的,它可以工作,但它似乎不是最简单的编写方式.有没有办法循环使用这些元素?
我曾经想象过这样的事情:
for (int i = 1; i <= 5; i++) {
Button(i).titleLabel.font = etc.. // How would I get the value of 'i'?
}
Run Code Online (Sandbox Code Playgroud)
或者这只是一个坏主意?
您可以使用NSArray迭代:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2571 次 |
| 最近记录: |