iOS 系统弹框展示扩展

Demo地址先上

 NSString *msg = @"1.如果你有女朋友\n2.请她吃东西?--你拉倒吧,吃胖了怎么办.\n3.恩.我想跟你谈恋爱?恩,你喜欢我,不关我事.\n4.追啊追...我的骄傲放纵";

系统的

iOS 系统弹框展示扩展_第1张图片
Snip20170720_1.png

然而,我想要居左订格的,比如用到版本更新提示的时候

UIAlertView

遍历subviews 即可获取msg对应的label,设置msglabel 居左属性即可
省事啊,可是没有margin ,效果贴边怎么办

NSInteger count = 0;
        for( UIView * view in alert.subviews )
        {
            if( [view isKindOfClass:[UILabel class]] )
            {
                count ++;
                if ( count == 2 ) { //仅对message左对齐
                    UILabel* label = (UILabel*) view;
                    //                    label.frame = CGRectMake(20, -20,200, size.height- 10);
                    label.textAlignment =NSTextAlignmentLeft;
                }
            }
        }

UIAlertController

UIAlertController获取msg所属的label,有一点点麻烦...不过总有父视图,sub...sub...sub 总会找到的

UIView *subView1 = alertController.view.subviews[0];
    UIView *subView2 = subView1.subviews[0];
    UIView *subView3 = subView2.subviews[0];
    UIView *subView4 = subView3.subviews[0];
    NSLog(@"subvie4 - %@",subView4.subviews);

    UIView *subView5 = subView4.subviews[0];
    NSLog(@"subvie5 - %@",subView5.subviews);

来,看下控制台输出的

subvie4 - (
    ">"
)
subvie5 - (
    ">",
    ">",
    ">",
    ">"
)

没错,多了两个label,找到这俩坑货了,别问我怎么区分谁是title ,谁是message的,没错---我试出来的.后面就可以随意居左,居中,居右了

UILabel *lab_title = subView5.subviews[0];
UILabel *lab_message = subView5.subviews[1];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
 [alertController addAction:cancelAction];
lab_message.textAlignment = NSTextAlignmentLeft;
lab_title.textAlignment = NSTextAlignmentCenter;
[vc presentViewController:alertController animated:YES completion:nil];
iOS 系统弹框展示扩展_第2张图片
Snip20170720_2.png

这样是不是好一些啦?不好?哦~
什么?你想要彩色的标题?什么?你想要大号的message?可以啊,后面随意啦~

//可富文本展示 -
     NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
     [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
     [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(3, 2)];
     [alertController setValue:alertControllerStr forKey:@"attributedTitle"];
     
//     修改message
     NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
     [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 2)];
     [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 4)];
     [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(5, 3)];
     [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
//     修改按钮的颜色
     [cancelAction setValue:[UIColor cyanColor] forKey:@"titleTextColor"];
iOS 系统弹框展示扩展_第3张图片
Snip20170720_3.png

不过这个样式有些固定,需要自己做range ,设置属性,烦的死!那就自定义吧,加载html富文本样式,后台返回,移动端直接加载,那就来吧.
搜了些自定义的alert ,发现挺喜欢customiOSAlert这个的,高度自定义很方便(html 富文本,计算高度先,想要滚动的就加scrollow,想跟系统更接近,就把动画去掉,按钮弄弄,多个按钮分割线,稍微改一下).下面是我项目中一些场景

NSString *htmlSr = @"

你好

这是一个例子,请显示

外加一个table

aaaabbbbcccc

"; CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init]; [alertView setContainerView:[self createView:htmlSr title:@"来"]]; [alertView setButtonTitles:@[@"确定",@"取消"]]; // [alertView setDelegate:self]; [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) { NSLog(@"sett -ldd- %d",buttonIndex); [alertView close]; }]; [alertView setUseMotionEffects:true]; [alertView show];
iOS 系统弹框展示扩展_第4张图片
Snip20170720_4.png

猿友们,拿去撸~

你可能感兴趣的:(iOS 系统弹框展示扩展)