UIAlertController

  • 通過runtime 修改AlertController的字體顏色 原文鏈接

自定義類 .h 文件

#import <UIKit/UIKit.h>

@interface SCAlertAction : UIAlertAction
@property (nonatomic,strong) UIColor *textColor; // 按鈕title字體顏色 
@end

@interface SCAlertController : UIAlertController
@property (nonatomic,strong) UIColor *tintColor; // 統(tǒng)一按鈕樣式
@property (nonatomic,strong) UIColor *titleColor; // 標題的顏色 
@property (nonatomic,strong) UIColor *messageColor; //信息的顏色
@end

自定義類 .m 文件

#import "SCAlertController.h"
#import <objc/runtime.h>

@implementation SCAlertAction

//按鈕標題的字體顏色
-(void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;

    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
    for(int i =0;i < count;i ++){

        Ivar ivar = ivars[i];
        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];

        if ([ivarName isEqualToString:@"_titleTextColor"]) {

            [self setValue:textColor forKey:@"titleTextColor"];
        }
    }
}

@end


@implementation SCAlertController

-(void)viewDidLoad
{
    [super viewDidLoad];

    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
    for(int i = 0;i < count;i ++){

        Ivar ivar = ivars[i];
        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];

        //標題顏色
        if ([ivarName isEqualToString:@"_attributedTitle"] && self.title && self.titleColor) {

            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:self.titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0]}];
            [self setValue:attr forKey:@"attributedTitle"];
        }

        //描述顏色
        if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && self.messageColor) {

            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:self.messageColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
            [self setValue:attr forKey:@"attributedMessage"];
        }
    }

    //按鈕如果沒設(shè)顏色斋竞,則統(tǒng)一用tintColor
    if (self.tintColor) {
        for (SCAlertAction *action in self.actions) {
            if (!action.textColor) {
                action.textColor = self.tintColor;
            }
        }
    }
}

@end

用法和父類的用法一致

SCAlertController *alertController = [SCAlertController alertControllerWithTitle:@"你確定要退出嗎?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
alertController.tintColor = [UIColor redColor]; //這里統(tǒng)一設(shè)置各個按鈕的顏色都為紅色.
alertController.titleColor = [UIColor redColor];

//取消
 SCAlertAction *cancelAction = [SCAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil];

//單獨修改一個按鈕的顏色
cancelAction.textColor = [UIColor greenColor];
[alertController addAction:cancelAction];

//退出
SCAlertAction *exitAction = [SCAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:exitAction];

 [self presentViewController:alertController animated:YES completion:nil];
  • 對UIAlertController 可以做一個封裝
+(void)alertShowTitle:(NSString*)title
               message:(NSString*)message
              sureText:(NSString*)sureText
             sureBlock:(void(^)())sureBlock
            cancelText:(NSString*)cancelText
           cancelBlock:(void(^)())cancelBlock
         alertVCsender:(UIViewController*)sender{

    if (!sender) {
       sender = [UIApplication sharedApplication].keyWindow.rootViewController;
    }
    
    UIAlertController *alertController =[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (sureBlock) {
             sureBlock();
        }
      }];
    [alertController addAction:sureAction];
    
    UIAlertAction *cancelAction = nil;
    if (cancelText.length > 0) {
      cancelAction = [UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if (cancelBlock) {
                cancelBlock();
            }
        }];
    [alertController addAction:cancelAction];
    }
    
    [sender presentViewController:alertController animated:YES completion:nil];
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末唯咬,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子问欠,更是在濱河造成了極大的恐慌俺榆,老刑警劉巖感昼,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異罐脊,居然都是意外死亡定嗓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門萍桌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宵溅,“玉大人,你說我怎么就攤上這事上炎∈崖撸” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵反症,是天一觀的道長辛块。 經(jīng)常有香客問我,道長铅碍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任线椰,我火速辦了婚禮胞谈,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘憨愉。我一直安慰自己烦绳,他們只是感情好,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布配紫。 她就那樣靜靜地躺著径密,像睡著了一般。 火紅的嫁衣襯著肌膚如雪躺孝。 梳的紋絲不亂的頭發(fā)上享扔,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機與錄音植袍,去河邊找鬼惧眠。 笑死,一個胖子當著我的面吹牛于个,可吹牛的內(nèi)容都是我干的氛魁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼秀存!你這毒婦竟也來了捶码?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤或链,失蹤者是張志新(化名)和其女友劉穎惫恼,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體株扛,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡尤筐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了洞就。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盆繁。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖旬蟋,靈堂內(nèi)的尸體忽然破棺而出油昂,到底是詐尸還是另有隱情,我是刑警寧澤倾贰,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布冕碟,位于F島的核電站,受9級特大地震影響匆浙,放射性物質(zhì)發(fā)生泄漏安寺。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一首尼、第九天 我趴在偏房一處隱蔽的房頂上張望挑庶。 院中可真熱鬧,春花似錦软能、人聲如沸迎捺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凳枝。三九已至,卻和暖如春跋核,著一層夾襖步出監(jiān)牢的瞬間岖瑰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工了罪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留锭环,地道東北人。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓泊藕,卻偏偏與公主長得像辅辩,于是被迫代替她去往敵國和親难礼。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容