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];
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 :
Using this approach we can achieve two things :
Note : This approach may only work with iOS 7.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With