美文网首页IOSiOS开发技巧
UIAlertController 修改message文字样式对

UIAlertController 修改message文字样式对

作者: 芝麻绿豆 | 来源:发表于2017-12-13 14:32 被阅读84次

    message默认显示是居中,现在需要修改为左对齐样式:
    利用runtime获取UIAlertController的属性:

            unsigned int count = 0;
            
            Ivar *property = class_copyIvarList([UIAlertController class], &count);
            
            for (int i = 0; i < count; i++) {
                
                Ivar var = property[i];
                
                const char *name = ivar_getName(var);
                
                const char *type = ivar_getTypeEncoding(var);
                
                NSLog(@"%s ====== %s",name,type);
                
            }
    

    可以设置message富文本(左对齐、字体、颜色):

            NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
            NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
            paragraph.alignment = NSTextAlignmentLeft;
            [alertControllerMessageStr setAttributes:@{NSParagraphStyleAttributeName:paragraph} range:NSMakeRange(0, alertControllerMessageStr.length)];
      
    

    利用runtime获取到属性:attributedMessage。使用KVO修改message文字样式对齐方式:

    [alertContro setValue:alertControllerMessageStr forKey:@"attributedMessage"]
    
    效果图

    相关文章

      网友评论

        本文标题:UIAlertController 修改message文字样式对

        本文链接:https://www.haomeiwen.com/subject/biwaixtx.html