2014-09-23 522 views
7

現在iOS8已棄用UIActionsheetUIAlertview iOS7上的自定義功能不再生效。到目前爲止,我知道的唯一定製是色調。而我所需要的是改變標題的字體大小和樣式,我還沒有找到任何方式與新的UIAlertAction是否可以編輯UIAlertAction標題字體大小和樣式?

Already referred to this但我仍然希望有一種方法來改變至少標題大小和字體。

提供你一些我的代碼爲UIAlertAction

UIAlertController * alertActionSheetController = [UIAlertController alertControllerWithTitle:@"Settings" 
                       message:@"" 
                     preferredStyle:UIAlertControllerStyleActionSheet]; 
    UIAlertAction * aboutTheAppAction = [UIAlertAction actionWithTitle:@"About the App" 
                   style:UIAlertActionStyleDefault 
                   handler:^(UIAlertAction * action){ 
                    NSLog(@"About the app"); 
                    [self openAbout]; 

                   }]; 


[alertActionSheetController addAction:aboutTheAppAction]; 
[self presentViewController:alertActionSheetController animated:YES completion:nil]; 
+0

看看這個https://github.com/ianb821/IBActionSheet – 2014-09-23 11:25:24

+0

那麼這會工作都因爲它是由UIView的一個新的子類,但我正在尋找的是如何編輯造型alertaction。 – Teffi 2014-09-24 02:39:55

回答

12

您可以更改UIAlertAction的字體和顏色。 首先,你需要添加的UILabel類別

@interface UILabel (FontAppearance) 
@property (nonatomic, copy) UIFont * appearanceFont UI_APPEARANCE_SELECTOR; 
@end 

@implementation UILabel (FontAppearance) 

-(void)setAppearanceFont:(UIFont *)font { 
    if (font) 
     [self setFont:font]; 
} 

-(UIFont *)appearanceFont { 
    return self.font; 
} 

@end 

類文件也被上傳於以下網址 https://www.dropbox.com/s/em91fh00fv3ut4h/Archive.zip?dl=0

導入文件中,您需要下面的函數調用後。

UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil]; 
[appearanceLabel setAppearanceFont:yourDesireFont]]; 

上面的代碼是在顏色和字體上測試的。並且只會對iOS8或更高版本有效。

+0

這將更改UIAlertController視圖中所有標籤的字體。應該接受 – p0lAris 2015-03-10 00:39:54

+0

作爲寫回答! – 2016-02-17 15:47:50

+0

謝謝爲我工作。我確實把它放到了我自己已經擁有的UILabel類中,並且屬性聲明進入了.h 我在.h中爲這些新選擇器添加了方法聲明 我導入了.h,我在其中使用UIAlertController – 2016-05-04 17:37:38

0

可以使用私有API更改警報操作的字體。它可能會讓你的應用程序被拒絕,我還沒有嘗試提交這樣的代碼。

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 

let action = UIAlertAction(title: "Some title", style: .Default, handler: nil)let attributedText = NSMutableAttributedString(string: "Some title") 

let range = NSRange(location: 0, length: attributedText.length) 
attributedText.addAttribute(NSKernAttributeName, value: 1.5, range: range) 
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "ProximaNova-Semibold", size: 20.0)!, range: range) 

alert.addAction(action) 

presentViewController(alert, animated: true, completion: nil) 

// this has to be set after presenting the alert, otherwise the internal property __representer is nil 
guard let label = action.valueForKey("__representer")?.valueForKey("label") as? UILabel else { return } 
label.attributedText = attributedText 
相關問題