iOS 自定義UIAlertController的title、message、button的顏色

有時設計要求改彈窗的顏色,so,改吧

#import <UIKit/UIKit.h>

@interface UIAlertAction (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color;

@end

@interface UIAlertController (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color;

- (void)zd_setAttributedTitle:(NSAttributedString *)title;

- (void)zd_setMessageColor:(UIColor *)color;

- (void)zd_setAttributedMessage:(NSAttributedString *)message;

- (void)zd_setActionTitleColor:(UIColor *)color;

@end
@implementation UIAlertAction (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color {
    [self setValue:color forKey:@"titleTextColor"];
}

@end

@implementation UIAlertController (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color {
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:color}];
    [self zd_setAttributedTitle:string];
}

- (void)zd_setAttributedTitle:(NSAttributedString *)title {
    [self setValue:title forKey:@"attributedTitle"];
}

- (void)zd_setMessageColor:(UIColor *)color {
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.message attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:color}];
    [self zd_setAttributedMessage:string];
}

- (void)zd_setAttributedMessage:(NSAttributedString *)message {
    [self setValue:message forKey:@"attributedMessage"];
}

- (void)zd_setActionTitleColor:(UIColor *)color {
    for (UIAlertAction *action in self.actions) {
        [action zd_setTitleColor:color];
    }
}

@end
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,455評論 25 708
  • 片段: 【R 原文片段】具備內化和應用知識的一個表現就是把附會的本能反應升級為界定新知、舊知二者邊界的理性反應,從...
    yyhill閱讀 278評論 0 0
  • C市的夏天總是那么炎熱,封喬頂著碩大的太陽,半瞇著眼睛,盯著因為太陽反光而看不清楚的字,隱約能看出是唐子發來...
    拂綠青煙閱讀 345評論 0 1
  • 1. 一個人的時候,時間,仿佛總是會過得比較快。 無所事事也好,忙碌也好,都能分外清晰地覺察到時間的流逝。 周末的...
    Silvie閱讀 215評論 6 0
  • 今天,媽媽出去散步。媽媽回來后我問了媽媽一個問題:給她推薦一個工作 工作一:每年1000美元 一年后每年加薪兩百;...
    絲城曼語閱讀 272評論 0 0