iOS AlertViewController的自定义文字颜色

AlertViewController的自定义文字颜色

alertController有时有显示的太单一,我们需要给他自定义颜色,这里我用的是NSMutableAttributedString,可以设置成自己喜欢的颜色和样式。。。

 UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"立即支付" message:@"您的应付金额为:¥58000" preferredStyle:UIAlertControllerStyleAlert];

    // 使用富文本来改变alerttitle字体大小和颜色

    NSMutableAttributedString *title =[[NSMutableAttributedString alloc]initWithString:@"立即支付"];

    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17]range:NSMakeRange(0,4)];

    [title addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]range:NSMakeRange(0,4)];

    [alert setValue:title forKey:@"attributedTitle"];

    // 使用富文本来改变alertmessage字体大小和颜色

    // NSMakeRange(0, 14) 代表:0位置开始 14个字符

    NSMutableAttributedString *message =[[NSMutableAttributedString alloc]initWithString:@"您的应付金额为:¥58000"];

    [message addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16]range:NSMakeRange(0,14)];

    [message addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:250/255.0 green:84/255.0 blue:88/255.0 alpha:1.0]range:NSMakeRange(8,6)];

    [alert setValue:message forKey:@"attributedMessage"];

    UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"立即代付" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnullaction){

        [selfnowPay];

    }];

//    //设置按钮的title颜色

    [cancelAction setValue:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]forKey:@"titleTextColor"];

//

//    //设置按钮的title的对齐方式

    [cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentCenter]forKey:@"titleTextAlignment"];

//

    UIAlertAction *okAction =[UIAlertAction actionWithTitle:@"朋友支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnullaction){

        [selffriendPay];

    }];

    // 设置按钮的title颜色

    [okAction setValue:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]forKey:@"titleTextColor"];

//

//    //设置按钮的title的对齐方式

    [okAction setValue:[NSNumber numberWithInteger:NSTextAlignmentCenter]forKey:@"titleTextAlignment"];

    [alert addAction:okAction];

    [alert addAction:cancelAction];

    [selfpresentViewController:alert animated:YEScompletion:nil];

}

//朋友代付

-(void)friendPay{

}

//立即支付

-(void)nowPay{

}

你可能感兴趣的:(iOS AlertViewController的自定义文字颜色)