iphone - 如何使循环异常?

标签 iphone ios objective-c

我的属性(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;

另一方面,我有这个循环:

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]];
    }
}

在那个循环中,我所有的 UILabel 都具有相同的字体大小,但我想在将来为 *labelTerms*labelVote 和其他一些标签做异常(exception)发展。

如何在循环中或在我的属性中创建此异常?提前致谢...

最佳答案

在您的 xib/storyboard 文件中设置每个 subview 的 标签。确保每个 您想要使用“ABeeZee-Regular”字体的 subview 有 1 个作为标签,对于您想要使用“Raleway-ExtraLight”字体的 subview 有 2 个:

enter image description here

确保没有带有标签 1 或 2 的 subview 不是标签/文本字段,那么代码可以简化为:

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]];
    }
}

或者更好:

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:如果您有很多 subview 并且您想要对它们进行分组,还(重新)考虑为您希望具有相同属性的每组 subview 使用数组的想法。

关于iphone - 如何使循环异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18746664/

相关文章:

ios - 内容可用推送通知在不调试时不调用委托(delegate)

iphone - iPhone 设备屏幕关闭模式下可以录制视频吗?

ios - UIButton 垂直对齐不起作用

iphone - 我试图在 xcode 中运行别人的项目,我得到了一个 "Build Failed"

android - PhoneGap 的推送消息

ios - 我们可以在 ios 图表中的带有图像的气球标记上添加多个值吗?

ios - 如何在 AppDelegate 中使用 NSUserDefaults

ios - UIView.animate with imageArray inside UIScrollView 但效果类似于分页启用

ios - 使用 Instagram API 获取特定的用户名数据

iphone - 如何在 iOS 中处理 1 到 3 个手指滑动手势