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

时间:2014-01-29 06:28:45

标签: ios iphone objective-c uialertview

在我的iOSproject中,我有一个UIAlertView,上面有3个按钮,但最后一个按钮文本默认为粗体字体,如何...,如何将第三个按钮文本的字体样式设置为正常? enter image description here

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

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

4 个答案:

答案 0 :(得分:2)

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

我创建了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中创建了这个MLKLoadingAlertView的实例,如下所示:

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

这将显示以下输出:

enter image description here

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

  1. 我们可以在iOS 7中为UIAlertView添加子视图
  2. 我们可以在iOS 7中更改按钮文字的字体
  3. 注意:此方法仅适用于iOS 7。

答案 1 :(得分:1)

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

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

enter image description here

答案 2 :(得分:0)

标准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]];

        }
}

how-to-change-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];
}

答案 3 :(得分:0)

如果您没有获得解决方案或遇到问题,请尝试自定义警报视图。这很容易使用打击我申请链接

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

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