博客> UIAlertController的title 和 message 自定义样式
UIAlertController的title 和 message 自定义样式
2017-10-19 10:21 评论:0 阅读:318 wangwei1
ios 自定义 UIAlertController的title message
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"解散学校需核实您的身份,请拨打 027-00000000" preferredStyle:UIAlertControllerStyleAlert];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];//调整行间距
    //修改alertController title
    NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"解散学校需核实您的身份,请拨打 027-00000000"];
    [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [[hogan string] length])];
      [hogan addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [[hogan string] length])];
    [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange([[hogan string] length] - 13, 13)];
    [hogan addAttribute:NSForegroundColorAttributeName value:COLOR(0, 120, 255, 1) range:NSMakeRange([[hogan string] length] - 13, 13)];
    [alert setValue:hogan forKey:@"attributedMessage"];
    //修改Title字体用[alert setValue:hogan forKey:@"attributedTitle"];
    UIView *subView1 = alert.view.subviews[0];
    UIView *subView2 = subView1.subviews[0];
    UIView *subView3 = subView2.subviews[0];
    UIView *subView4 = subView3.subviews[0];
    UIView *subView5 = subView4.subviews[0];
    //        取title和message:
    //        UILabel *title = subView5.subviews[0];
    UILabel *message = subView5.subviews[1];
    //        然后设置message内容居左:
    message.textAlignment = NSTextAlignmentCenter;
    [message yb_addAttributeTapActionWithStrings:@[@"027-84210006"] delegate:self];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            
    }];
    //  更改alertController所有label样式
    //        UILabel *appearanceLabel = [UILabel appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]];
    //        [appearanceLabel setAppearanceFont:[UIFont systemFontOfSize:8]];        
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
收藏
1
sina weixin mail 回到顶部