美文网首页
ios ~ UIAlertcontroller 设置字体颜色

ios ~ UIAlertcontroller 设置字体颜色

作者: 阳光下的叶子呵 | 来源:发表于2022-01-15 10:53 被阅读0次

    ios 系统提示框 改变字体

    UIAlertView

    一般使用提示框,会使用“UIAlertView”!

    首先遵守<UIAlertViewDelegate>协议!

    UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"标题" message:@"真的要退出此账号么?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertV show]; //展示提示框
    复制代码
    

    效果:

    [图片上传失败...(image-b5adbf-1642215038552)]

    使用“UIAlertViewDelegate”,响应 按钮点击事件!

    #pragma mark - UIAlertViewDelegate
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //按钮点击
    
      if (buttonIndex == 1) { //确认按钮
          //确认的操作
    
      }
    
    }
    复制代码
    

    可通过更改UIAlertView的alertViewStyle属性来实现文字输入的效果。

    UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"发到邮箱" message:@"请输入邮箱地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil]; 
    //设置 含有文字输入框
    [alertV setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
    [alertV show];
    
    //获取文字输入框(UITextField *)
    UITextField * textStrTF = [alertV textFieldAtIndex:0];
    textStrTF.keyboardType = UIKeyboardTypeEmailAddress; //邮箱键盘
    textStrTF.placeholder = @"请输入邮箱地址";
    textStrTF.text = [User_Def objectForKey:USER_EMail]?[User_Def objectForKey:USER_EMail]:@"";
    textStrTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时有删除按钮
    复制代码
    

    效果:

    [图片上传失败...(image-74266c-1642214646018)]



    UIAlertController

    改变字体(富文本)的颜色大小时,最好使用UIAlertController! (KVC方式 [setValue: forKey:]

    • 改变 按钮字体颜色
      NSString * titleStr = NSLocalizedString(@"标题", nil); //标题
      
      UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle: titleStr message:@"真的要退出此账号么?" preferredStyle:UIAlertControllerStyleAlert];
      
      //确认按钮
      UIAlertAction *sureAction=[UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          //确认的操作
      
      }];
      UIColor * changeColor = [UIColor redColor]; //颜色
      [sureAction setValue: changeColor forKey:@"titleTextColor"]; //KVC:改变字体颜色
      
      //取消按钮
      UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          //取消的操作
      
      }];
      //[cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"]; //KVC:改变字体颜色
      
      //将UIAlertAction项 添加到UIAlertController
      [alertcontroller addAction:cancelAction];
      [alertcontroller addAction: sureAction];
      
      //present 显示alertcontroller
      [self presentViewController:alertcontroller animated:YES completion:nil];
      复制代码
      

      效果:

      [图片上传失败...(image-1ab5f6-1642214646018)]

    • 改变标题、及提示信息的字体:(富文本)
      //改变title的大小和颜色
      NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:titleStr];
      [titleAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.f] range:NSMakeRange(0, titleStr.length)];
      [titleAtt addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, titleStr.length)];
      //KVC:改变字体(富文本)
      [alertcontroller setValue:titleAtt forKey:@"attributedTitle"];
      
      NSString * messageStr = @"真的要退出此账号么?";
      //改变message的大小和颜色
      NSMutableAttributedString *messageAtt = [[NSMutableAttributedString alloc] initWithString:messageStr];
      [messageAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.f] range:NSMakeRange(0, messageStr.length)];
      [messageAtt addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(0, messageStr.length)];
      //KVC:改变字体(富文本)
      [alertcontroller setValue:messageAtt forKey:@"attributedMessage"];
      复制代码
      

      效果:

      [图片上传失败...(image-a891f4-1642214646018)]

    UIAlertController添加 文字输入框

    [alertcontroller addTextFieldWithConfigurationHandler:^(UITextField *textField){
       textField.placeholder = @"输入文字";
       
    }];
    复制代码
    

    效果

    [图片上传失败...(image-a2d866-1642214646018)]

    表格形式

    preferredStyle设置为UIAlertControllerStyleActionSheet! (表格形式

    效果: 在屏幕的最底部




    使用KVC方式 ([setValue: forKey:] ),改变字体的大小和颜色!!

    相关文章

      网友评论

          本文标题:ios ~ UIAlertcontroller 设置字体颜色

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