如何显示在UIAlertView在iOS上的所有button正常字体的文本

在我的iOS项目,我有一个UIAlertView与3个button,但最后一个button的文本默认为粗体字体,像这样…我怎样才能设置正常的第三个button文字的字体样式? 在这里输入图像说明

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

 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"test" message:@"testTestTest" delegate:Nil cancelButtonTitle:nil otherButtonTitles:@"first",@"second",@"third" ,nil]; [alert show]; 

在UIAlertView呈现时,我花了一些时间在这个视图控制器的子视图结构上进行挖掘。 我能够显示所有button文本的正常字体。

我已经创build了UIAlertView的一个子类:

头文件:

 #import <UIKit/UIKit.h> @interface MLKLoadingAlertView : UIAlertView - (id)initWithTitle:(NSString *)title; @end 

实施文件:

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

我在我的View Controller中创build了这个MLKLoadingAlertView的实例,如下所示:

  MLKLoadingAlertView *loadingAlertView = [[MLKLoadingAlertView alloc] initWithTitle:TITLE]; [loadingAlertView show]; 

这将显示以下输出:

在这里输入图像说明

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

  1. 我们可以在iOS 7中添加子视图到UIAlertView
  2. 我们可以在iOS 7中更改button文字的字体

注意 :这种方法可能只适用于iOS 7。

cancelButtonTitle取消button的标题为空,它将显示如下图像

  UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"This is your first UIAlertview message." delegate:self cancelButtonTitle:@" " otherButtonTitles:@"Button 1",@"Button 2", @"Button 3", nil]; [message show]; 

在这里输入图像说明

标准UI控件的未公开的子视图结构

 @interface:UIViewController<UIAlertViewDelegate> - (void)willPresentAlertView:(UIAlertView *)alertView{ for(UIView *view in alertView.subviews) if(([view isKindOfClass:[UIButton class]]) && (view.tag ==3)){ UIButton *myButton = (UIButton *)view; [myButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]]; } } 

要么

如何对变化UIAlertView中,fontcolors

 -(void)willPresentAlertView:(UIAlertView *)a;ertView{ UILabel *title = [alertView valurForKey:@"_titleLabel"]; title.font = [UIFont fontWithName:@"Arial" size:18]; [title setTextColor:[UIColor whiteColor]; UILabel *body = [alertView valueForKey:@"_bodyTextLabel"]; body.font = [UIFont fontWithName:@"Arial" size:15]; [body setTextColor:[UIColor whiteColor]; } 

如果您没有得到解决scheme或面临问题,然后尝试自定义警报视图。 这是非常容易使用吹我申请链接

https://www.cocoacontrols.com/controls/pxalertview

https://www.cocoacontrols.com/controls/sialertview