How to change UIAlertView text color?
I'm currently using http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color fo开发者_开发技巧r my custom UIAlertView. Everything is working fine but I'm trying to change the title and message text colors and I would like to change the button color. Is there any way to do this? I've tried using:
UILabel *theTitle = [AlertView valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
but I can't get it to work. Any help would be appreciated. Thanks.
You just need to include UIAlertViewDelegate
in your .h
file and override this method in your .m
file.
-(void)willPresentAlertView:(UIAlertView *)alertView{
UILabel *theTitle = [alertView valueForKey:@"_titleLabel"];
theTitle.font = [UIFont fontWithName:@"Copperplate" size:18];
[theTitle setTextColor:[UIColor whiteColor]];
UILabel *theBody = [alertView valueForKey:@"_bodyTextLabel"];
theBody.font = [UIFont fontWithName:@"Copperplate" size:15];
[theBody setTextColor:[UIColor whiteColor]];
}
Check to see if setTextColor is depricated.
I thought it was
theTitle.text.color = [UIColor redColor];
or you could use
[theTitle.text setColor:[UIColor redColor]];
Not totally sure on the second one, the first example should do the trick though.
By default functionality, you could not change alert view title and message but with the help of accessory view, you can achieve this functionality. Try below method :
-(void)customAlertTitle:(NSString*)title message:(NSString*)message{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 270, 50)];
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.numberOfLines = 2;
titleLabel.textColor = [UIColor redColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
[subView addSubview:titleLabel];
UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 270, 50)];
messageLabel.text = message;
messageLabel.font = [UIFont systemFontOfSize:18];
messageLabel.numberOfLines = 2;
messageLabel.textColor = [UIColor redColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
[subView addSubview:messageLabel];
[alertView setValue:subView forKey:@"accessoryView"];
[alertView show];
}
The above code is working perfectly on latest XCode 8.3.1. Kindly comment if facing any problem.
精彩评论