Home > 移动互联, 语言编程 > iOS开发之自定义系统的UIAlertView调整Alert字体

iOS开发之自定义系统的UIAlertView调整Alert字体

iOS中UIAlertView很少有单独定制的需要,一般采用系统样式即可,但是有时候一些特殊的需求(比如:UIAlertView的字体,字体大小,字体对齐方式变化等)就不得不需要单独对UIAlertView进行定制了。
定制的方法也很简单,在viewController的Delegate实现方法willPresentAlertView中遍历UIAlertView下面所有subview,找到对应的UILabel再对UILabel的属性进行修改即可。操作基本一致。


参考代码;

- (void)willPresentAlertView:(UIAlertView *)alertView {
    // 遍历 UIAlertView 所包含的所有控件
    for (UIView *tempView in alertView.subviews) {
 
        if ([tempView isKindOfClass:[UILabel class]]) {
            // 当该控件为一个 UILabel 时
            UILabel *tempLabel = (UILabel *) tempView;
 
            if ([tempLabel.text isEqualToString:alertView.message]) {
                // 调整对齐方式
                tempLabel.textAlignment = UITextAlignmentLeft;
                // 调整字体大小
                [tempLabel setFont:[UIFont systemFontOfSize:15.0]];
            }
        }
    }
}

以上代码不会出现使用了私有API的问题

Categories: 移动互联, 语言编程 Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.