如何在ios中的UIAlertView上以普通字体显示所有按钮文本

Rav*_*ran 5 iphone objective-c uialertview ios

在我的iOSproject中,我有一个UIAlertView带有3个按钮,但最后一个按钮文本默认为粗体字体,如此...,如何将第三个按钮文本的字体样式设置为正常? 在此输入图像描述

以下是用于创建该警报视图的代码

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"test" message:@"testTestTest" delegate:Nil cancelButtonTitle:nil otherButtonTitles:@"first",@"second",@"third" ,nil];
    [alert show];
Run Code Online (Sandbox Code Playgroud)

Nag*_*ali 2

我花了一些时间在这上面,并在呈现 UIAlertView 时深入研究当前视图控制器的子视图结构。我能够向所有按钮文本显示正常字体。

我创建了 UIAlertView 的子类:

头文件:

#import <UIKit/UIKit.h>

@interface MLKLoadingAlertView : UIAlertView

- (id)initWithTitle:(NSString *)title;

@end
Run Code Online (Sandbox Code Playgroud)

实施文件:

#import "MLKLoadingAlertView.h"

#define ACTIVITY_INDICATOR_CENTER   CGPointMake(130, 90)

@implementation MLKLoadingAlertView

- (id)initWithTitle:(NSString *)title
{
    if ( self = [super init] )
    {
        self.title = title;
        self.message = @"\n\n";
        [self addButtonWithTitle:@"OK"];
        [self addButtonWithTitle:@"Cancel"];
        [self addButtonWithTitle:@"Sure"];

        [self setDelegate:self];
    }

    return self;
}

// You can Customise this based on your requirement by adding subviews.
- (void)didPresentAlertView:(UIAlertView *)alertView
{
    NSArray *subviews = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController.view.subviews;

    if( subviews.count > 1 )
    {
        // iOS while presenting an alertview uses a presening view controller. That controller's view has several subviews. I have picked one
        // subview from it which has frame similar to the alertview frame.
        UIView *presentedView = [subviews objectAtIndex:1];

        for( UIView *view in presentedView.subviews )
        {
            for( UIView *subView in view.subviews )
            {
                if( [subView isKindOfClass:[UITableView class]] )
                {
                    NSInteger numberOfButtons = [(UITableView *)subView numberOfRowsInSection:0];
                    UITableViewCell *buttonCell = [(UITableView *)subView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:numberOfButtons-1 inSection:0]];


                    for( UIView *cellSubView in buttonCell.contentView.subviews )
                    {
                        if( [cellSubView isKindOfClass:[UILabel class]] )
                        {
                            [(UILabel *)cellSubView setFont:[UIFont systemFontOfSize:17]];
                        }
                    }

                    break;
                }
            }
        }

        UIActivityIndicatorView *customActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [customActivityIndicator startAnimating];
        customActivityIndicator.center = ACTIVITY_INDICATOR_CENTER;

        [presentedView addSubview:customActivityIndicator];
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的视图控制器中创建了这个 MLKLoadingAlertView 的实例,如下所示:

    MLKLoadingAlertView *loadingAlertView = [[MLKLoadingAlertView alloc] initWithTitle:TITLE];
    [loadingAlertView show];
Run Code Online (Sandbox Code Playgroud)

这将显示以下输出:

在此输入图像描述

使用这种方法我们可以实现两件事:

  1. 我们可以在 iOS 7 中向 UIAlertView 添加子视图
  2. 我们可以在 iOS 7 中更改按钮文本的字体

注意:此方法可能仅适用于 iOS 7。