NSString *msg = @"1.如果你有女朋友\n2.請她吃東西?--你拉倒吧,吃胖了怎么辦.\n3.恩.我想跟你談戀愛?恩,你喜歡我,不關(guān)我事.\n4.追啊追...我的驕傲放縱";
系統(tǒng)的
然而,我想要居左訂格的,比如用到版本更新提示的時候
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,找到這倆坑貨了,別問我怎么區(qū)分誰是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];
這樣是不是好一些啦?不好?哦~
什么?你想要彩色的標題?什么?你想要大號的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"];
不過這個樣式有些固定,需要自己做range ,設置屬性,煩的死!那就自定義吧,加載html富文本樣式,后臺返回,移動端直接加載,那就來吧.
搜了些自定義的alert ,發(fā)現(xiàn)挺喜歡customiOSAlert這個的,高度自定義很方便(html 富文本,計算高度先,想要滾動的就加scrollow,想跟系統(tǒng)更接近,就把動畫去掉,按鈕弄弄,多個按鈕分割線,稍微改一下).下面是我項目中一些場景
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];
猿友們,拿去擼~