banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

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 的问题

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.