zoukankan      html  css  js  c++  java
  • UIAlertController 修改文字显示实现方法

    UIAlertController修改文字显示

    不废话先上完整代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];
        //修改标题
        NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc] initWithString:@"提示"];
        [attrTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, [[attrTitle string] length])];
        [attrTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[attrTitle string] length])];
        [alertController setValue: attrTitle forKey:@"attributedTitle"];
        //修改message
        NSMutableAttributedString * attrMessage = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
        [attrMessage addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, [[attrMessage string] length])];
        [attrMessage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [[attrMessage string] length])];
        [alertController setValue: attrMessage forKey:@"attributedMessage"];
        //修改按钮的颜色,同上可以使用同样的方法修改内容,样式
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [defaultAction setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
        [cancelAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];
     
        [alertController addAction:defaultAction];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];

    修改标题属性

    key: attributedTitle

    1
    2
    3
    4
    5
    //修改标题的内容,字号,颜色。使用的key值是“attributedTitle"
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"标题"];
        [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:NSMakeRange(0, [[attr string] length])];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[hogan string] length])];
        [alertController setValue:hogan forKey:@"attributedTitle"];

    修改内容属性

    key: attributedMessage

    1
    2
    3
    4
    5
    //修改message
        NSMutableAttributedString * attrMessage = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
        [attrMessage addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, [[attrMessage string] length])];
        [attrMessage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [[attrMessage string] length])];
        [alertController setValue: attrMessage forKey:@"attributedMessage"];

    标题AlertAction按钮字体颜色

    key: _titleTextColor或者titleTextColor

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //修改按钮的颜色,同上可以使用同样的方法修改内容,样式
       UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
       UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
     
       [defaultAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
       [cancelAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
     
       [alertController addAction:defaultAction];
       [alertController addAction:cancelAction];

    效果图

  • 相关阅读:
    Maven pom.xml文件获取当前时间戳
    maven依赖jdk的tools.jar包坐标怎么依赖
    解决Git问题:git登录账号密码错误remote: Incorrect username or password、如何快速关联/修改Git远程仓库地址、git修改用户名邮箱密码
    浅析Java里的内存分析及常量池加强对Java里字符串的理解
    浅析Java数据结构:稀疏数组的介绍和使用场景
    UmiJS简单介绍及使用UmiJS开发结构浅析
    React基础知识笔记:如何渲染html代码、条件渲染与循环渲染、如何获取动态路由传参、动态设置背景图、umi+dva中全局使用dispatch
    git commit提交时报错husky > pre-commit (node v10.16.3)
    浅析npm报错ENOTFOUND npm ERR! network request to https://npm.***.com/*** failed 及 .npmrc 文件的作用、npm --verbose命令
    浅析为什么说Java中只有值传递
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/11428127.html
Copyright © 2011-2022 走看看