如何在ios中的UIAlertView上以普通字体显示所有按钮文本 [英] How do I show all the buttons text with normal font on UIAlertView in ios

查看:121
本文介绍了如何在ios中的UIAlertView上以普通字体显示所有按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOSproject中,我有一个 UIAlertView ,上面有3个按钮,但是最后一个按钮文本默认为粗体字体,这样......,我怎样才能设置第三个按钮文字的字体样式正常吗?

In my iOSproject I have an UIAlertView with 3 buttons on it , but the last button text is defaulted to bold font like this... , how can I set the font style of third button text to normal ?

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

following is the code used to create that alert view

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


推荐答案

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

I spent some time on this and went to dig along subview structure of present view controller when UIAlertView is presented. I am able to show normal font to all button text.

我创建了UIAlertView的子类:

I have created a subclass of 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的实例像这样:

I have created instance of this MLKLoadingAlertView in my View Controller like this :

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

这将显示以下输出:

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

Using this approach we can achieve two things :


  1. 我们可以在iOS 7中为UIAlertView添加子视图

  2. 我们可以更改字体iOS 7中按钮文本的设置

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

Note : This approach may only work with iOS 7.

这篇关于如何在ios中的UIAlertView上以普通字体显示所有按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆