2015-04-01 88 views
3

https://www.safaribooksonline.com/library/view/ios-8-swift/9781491908969/images/ispc_0113.png如何在上面如何更改字体大小和“完成”,“其他一些行动”颜色的图像改变UIAlertAction的字体大小和颜色UIAlertController

? 以及如何更改“标题”和“消息”的字体大小和颜色?

谢谢。

+1

[UIAlertController自定义字体,大小,颜色](HTTP的可能重复://计算器。 com/questions/26460706/uialertcontroller-custom-font-size-color) – picciano 2015-04-03 17:59:14

+0

检查此解决方案http://stackoverflow.com/a/27518769/2050181 – 2015-12-02 11:46:07

+0

检查此解决方案http://stackoverflow.com/questions/26460706/uialertcontroller - 自定义字体大小的颜色/ 41299305#41299305 – 2016-12-23 10:56:25

回答

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:霍根forKey:@ “attributedTitle”];

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

而且你会得到一个崩溃试图设置一些ios版本的未定义键值(不要r emember)。您还必须为您要设置图像的UIAlertAction重写'setValue:forUndefinedKey:'和'valueForUndefinedKey:'。 – vahotm 2017-11-09 12:06:21

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"]; 
      } 
     } 
    } 
0

一个动作我无法改变字体,但你可以改变字体颜色。从'霍根例如,改变上UIAlertAction字体颜色:

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

只需做这样

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

     // TODO : ACTION 
    }]; 

    [action setValue:[UIColor redColor] forKey:@"titleTextColor"]; 
    [alertController action]; 
相关问题