【iOS】UIAlertController顏色字號更改

做開發(fā)這么久,見多太多的設(shè)計(jì)圖,能用系統(tǒng)解決UI的堅(jiān)決不麻煩,今天回想起剛?cè)胄械哪莻€(gè)時(shí)候,年少無知啊,說說咱們常用的alertview,iOS8以后改為UIAlertController了,那么用到UIAlertController的地方難免有些字體啊,顏色啊不那么讓需求滿意,那好吧,title字號改大,message字號改小,顏色還能不能改一下,這么一點(diǎn)需求,定制似乎不太值得吧,但又沒有設(shè)置的屬性,咋辦,這時(shí)候想起KVC,很簡單解決需求;
示例圖:


Snip20170216_2.png

代碼如下:

NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc]initWithString:@"標(biāo)題1" attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    NSMutableAttributedString *attMessage = [[NSMutableAttributedString alloc]initWithString:@"message" attributes:@{NSForegroundColorAttributeName:[UIColor purpleColor],NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    
    UIAlertController *action = [UIAlertController alertControllerWithTitle:@"標(biāo)題1" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
    [action setValue:attTitle forKey:@"attributedTitle"];
    [action setValue:attMessage forKey:@"attributedMessage"];
    
    UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"拍攝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadCameraMovie];
    }];
    [alert1 setValue:[UIColor greenColor] forKey:@"titleTextColor"];
    [action addAction:alert1];
    
    UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadCamera];
    }];
    [alert2 setValue:[UIColor cyanColor] forKey:@"titleTextColor"];
    [action addAction:alert2];
    
    UIAlertAction *alert3 = [UIAlertAction actionWithTitle:@"從相冊選擇視頻" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadPhotoLibraryMovie];
    }];
    [alert3 setValue:[UIColor orangeColor] forKey:@"titleTextColor"];
    [action addAction:alert3];
    
    UIAlertAction *alert4 = [UIAlertAction actionWithTitle:@"從相冊選擇照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadPhotoLibraryPhoto];
    }];
    [alert4 setValue:[UIColor brownColor] forKey:@"titleTextColor"];
    [action addAction:alert4];
    
    UIAlertAction *alert5 = [UIAlertAction actionWithTitle:@"從相冊選擇多張照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadQBImagePickerController];
    }];
    [alert5 setValue:[UIColor blackColor] forKey:@"titleTextColor"];
    [action addAction:alert5];
    
    UIAlertAction *can = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [can setValue:[UIColor redColor] forKey:@"titleTextColor"];
    [action addAction:can];
    [self presentViewController:action animated:YES completion:nil];

有的朋友可能納悶時(shí)如何獲取的這些屬性key,這里大家可以使用runtime

unsigned int count = 0;
    Ivar *ivars = class_copyIvarList(NSClassFromString(@"CYObject"), &count);
    //ivars不是數(shù)組而是內(nèi)存地址
    NSLog(@"count:%d",count);
    for (int i = 0; i < count; i++) {
        //獲取成員變量
        Ivar ivar = ivars[i];
        const char *name = ivar_getName(ivar);
        NSString *sname = [NSString stringWithUTF8String:name];
        NSLog(@"name:%@",sname);
    }
    free(ivars);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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