美文网首页
iOS 系统弹框展示扩展

iOS 系统弹框展示扩展

作者: 懂妹儿 | 来源:发表于2017-08-11 14:33 被阅读0次

Demo地址先上

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

系统的

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 - (
    "<UIView: 0x7ff520f09e60; frame = (0 0; 0 0); layer = <CALayer: 0x608000230a80>>"
)
subvie5 - (
    "<UILabel: 0x7ff520f0a390; frame = (0 0; 0 0); text = '\U4e0d\U4f1a\U8ffd,\U6211\U6559\U4f60\U554a'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x608000284ba0>>",
    "<UILabel: 0x7ff520f0aaa0; frame = (0 0; 0 0); text = '1.\U5982\U679c\U4f60\U6709\U5973\U670b\U53cb\n2.\U8bf7\U5979\U5403\U4e1c\U897f?--\U4f60\U62c9\U5012\U5427,...'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x608000284ce0>>",
    "<UIView: 0x7ff520f0af40; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x6080002309c0>>",
    "<UIView: 0x7ff520f0b0e0; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x60800022d7e0>>"
)

没错,多了两个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];
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"];
Snip20170720_3.png

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

NSString *htmlSr = @"<p>你好</p><p>        这是一个例子,请显示</p><p>外加一个table</p><table><tbody><tr class=\"firstRow\"><td valign=\"top\" width=\"261\">aaaa</td><td valign=\"top\" width=\"261\">bbbb</td><td valign=\"top\" width=\"261\">cccc</td></tr></tbody></table><p></p>";
    
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];
Snip20170720_4.png

猿友们,拿去撸~

相关文章

网友评论

      本文标题:iOS 系统弹框展示扩展

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