最近做了一個(gè)提示版本更新的功能火惊,發(fā)現(xiàn)UIAlertView上的文字居中顯示唯欣,真的好丑啊居触,然后在網(wǎng)上搜了好多如何居左顯示信息,大多數(shù)說的是用一下方法(但是在iOS7之后就不能使用了)
- (void)willPresentAlertView:(UIAlertView*)alertView{
UIView* view = [alertView.subviewsobjectAtIndex:2];
if([viewisKindOfClass:[UILabelclass]]){
UILabel* label = (UILabel*) view;
label.textAlignment=NSTextAlignmentLeft;
}
}
效果圖:
于是看了很多文章后決定自己做一個(gè)自定義的痹屹,實(shí)現(xiàn)了效果章郁,代碼如下:
UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"有新版本發(fā)布啦!"message:messagedelegate:selfcancelButtonTitle:@"忽略"otherButtonTitles:@"前往更新",nil];
//
alertView.tag=1243;
//如果你的系統(tǒng)大于等于6.0
if(floor(NSFoundationVersionNumber) >NSFoundationVersionNumber_iOS_6_1)
{
//計(jì)算文字的尺寸大小
CGSizesize = [messagesizeWithFont:[UIFontsystemFontOfSize:15]constrainedToSize:CGSizeMake(240,400)lineBreakMode:NSLineBreakByCharWrapping];
UILabel*textLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,240, size.height)];
textLabel.font= [UIFontsystemFontOfSize:15];
textLabel.textColor= [UIColorblackColor];
textLabel.backgroundColor= [UIColorclearColor];
textLabel.lineBreakMode=NSLineBreakByCharWrapping;
textLabel.numberOfLines=0;
textLabel.textAlignment=NSTextAlignmentLeft;
//? ? ? ? ? ? ? ? ? ? ? ? textLabel.layer.borderWidth=1;
//? ? ? ? ? ? ? ? ? ? ? ? textLabel.layer.borderColor=[UIColor grayColor].CGColor;
textLabel.text= message;
//添加label在alerView上
[alertViewsetValue:textLabelforKey:@"accessoryView"];
//清空alertview上的message
alertView.message=@"";
}
[alertViewshow];
//ios8UIAlertController的設(shè)置方法
UIView *subView1 = [alertCn.view.subviews firstObject];
UIView *subView2 = [subView1.subviews firstObject];
UIView *subView3 = [subView2.subviews firstObject];
UIView *subView4 = [subView3.subviews firstObject];
UIView *subView5 = [subView4.subviews firstObject];
UILabel *message = subView5.subviews[1];
message.textAlignment = NSTextAlignmentLeft;