2015-04-01 84 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]; 
相關問題