【iOS】UIAlertController颜色字号更改

做开发这么久,见多太多的设计图,能用系统解决UI的坚决不麻烦,今天回想起刚入行的那个时候,年少无知啊,说说咱们常用的alertview,iOS8以后改为UIAlertController了,那么用到UIAlertController的地方难免有些字体啊,颜色啊不那么让需求满意,那好吧,title字号改大,message字号改小,颜色还能不能改一下,这么一点需求,定制似乎不太值得吧,但又没有设置的属性,咋办,这时候想起KVC,很简单解决需求;
示例图:


【iOS】UIAlertController颜色字号更改_第1张图片
Snip20170216_2.png

代码如下:

NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc]initWithString:@"标题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:@"标题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];

有的朋友可能纳闷时如何获取的这些属性key,这里大家可以使用runtime

unsigned int count = 0;
    Ivar *ivars = class_copyIvarList(NSClassFromString(@"CYObject"), &count);
    //ivars不是数组而是内存地址
    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);

你可能感兴趣的:(【iOS】UIAlertController颜色字号更改)