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 :
- We can add subviews to UIAlertView in iOS 7
- We can change font of button text in iOS 7
Note : This approach may only work with iOS 7.
cancelButtonTitle The title of the cancel button as empty it will show like below image
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];
undocumented subview structure of a standard UI control
@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]];
}
}
or
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];
}
If you are not getting solution or facing problem then try to custom alert view. that are very easy to use blow i apply link
来源:https://stackoverflow.com/questions/21423681/how-do-i-show-all-the-buttons-text-with-normal-font-on-uialertview-in-ios