Как изменить цвет текста UIAlertView? - PullRequest
0 голосов
/ 21 января 2011

В настоящее время я использую http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color для своего пользовательского UIAlertView. Все работает нормально, но я пытаюсь изменить заголовок и цвет текста сообщения, и я хотел бы изменить цвет кнопки. Есть какой-либо способ сделать это? Я пробовал использовать:

UILabel *theTitle = [AlertView valueForKey:@"_titleLabel"];
    [theTitle setTextColor:[UIColor redColor]];

но я не могу заставить его работать. Любая помощь будет оценена. Спасибо.

Ответы [ 3 ]

1 голос
/ 25 августа 2012

Вам просто нужно включить UIAlertViewDelegate в файл .h и переопределить этот метод в файле .m.

-(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]];
}
0 голосов
/ 27 апреля 2017

По умолчанию вы не можете изменить заголовок и сообщение представления оповещения, но с помощью вспомогательного представления вы можете достичь этой функции. Попробуйте метод ниже:

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

Приведенный выше код отлично работает на последней версии XCode 8.3.1. Просьба прокомментировать, если возникнут какие-либо проблемы.

0 голосов
/ 21 января 2011

Проверьте, не установлен ли setTextColor.

Я думал, что это

theTitle.text.color = [UIColor redColor];

или вы можете использовать

[theTitle.text setColor:[UIColor redColor]];

Не совсем уверен во втором, хотя первый пример должен помочь.

...