Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show all the buttons text with normal font on UIAlertView in ios

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 ? enter image description here

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];
like image 925
Ravi Kiran Avatar asked Jan 29 '14 06:01

Ravi Kiran


1 Answers

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.

I have created a subclass of UIAlertView :

Header File :

#import <UIKit/UIKit.h>

@interface MLKLoadingAlertView : UIAlertView

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

@end

Implementation File :

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

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

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

This will show following output :

enter image description here

Using this approach we can achieve two things :

  1. We can add subviews to UIAlertView in iOS 7
  2. We can change font of button text in iOS 7

Note : This approach may only work with iOS 7.

like image 110
Naga Mallesh Maddali Avatar answered Oct 14 '22 17:10

Naga Mallesh Maddali