如何在UIAlertController中更改UIAlertAction的字体大小和颜色

时间:2015-04-01 02:46:53

标签: ios iphone uialertcontroller uialertaction

https://www.safaribooksonline.com/library/view/ios-8-swift/9781491908969/images/ispc_0113.png

在上图中如何更改“完成”,“其他动作”的字体大小和颜色? 以及如何更改“标题”和“消息”的字体大小和颜色?

谢谢。

4 个答案:

答案 0 :(得分:6)

这是来自另一个堆栈溢出帖子,但似乎工作正常。 Post found here.

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
          value:[UIFont systemFontOfSize:50.0]
          range:NSMakeRange(24, 11)];
[alertVC setValue:hogan forKey:@"attributedTitle"];



UIAlertAction *button = [UIAlertAction actionWithTitle:@"Label text" 
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction *action){
                                                //add code to make something happen once tapped
}];
UIImage *accessoryImage = [UIImage imageNamed:@"someImage"];
[button setValue:accessoryImage forKey:@"image"];

要使用以下内容更改文本的颜色,请在行设置之前添加 [alertVC setValue:hogan forKey:@“attributionTitle”];

[hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,35)];

答案 1 :(得分:4)

在UIAlertController

中更改颜色动作
SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{
    UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];

    if ([alertController isKindOfClass:[UIAlertController class]])
    {
        NSArray *arr = alertController.actions;
        for (int i = 0; i <arr.count; i ++) {
            UIAlertAction *alertAction = [arr objectAtIndex:i];
            if ([alertAction.title isEqualToString:@"Logout"]) {
                UIColor *color = [UIColor redColor];
                [alertAction setValue:color forKey:@"titleTextColor"];
            }
        }
    }

答案 2 :(得分:1)

我无法更改字体,但您可以更改字体颜色。从'hogan'示例中,更改UIAlertAction上的字体颜色:

[button setValue:[UIColor greenColor] forKey:@"titleTextColor"];

答案 3 :(得分:1)

只需这样做

    UIAlertAction * action = [UIAlertAction actionWithTitle:@"ACTION TITLE" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

       // TODO : ACTION
    }];

    [action setValue:[UIColor redColor] forKey:@"titleTextColor"];
    [alertController action];