最近做了一个提示版本更新的功能,发现UIAlertView上的文字居中显示,真的好丑啊,然后在网上搜了好多如何居左显示信息,大多数说的是用一下方法(但是在iOS7之后就不能使用了)
- (void)willPresentAlertView:(UIAlertView*)alertView{
UIView* view = [alertView.subviewsobjectAtIndex:2];
if([viewisKindOfClass:[UILabelclass]]){
UILabel* label = (UILabel*) view;
label.textAlignment=NSTextAlignmentLeft;
}
}
效果图:
于是看了很多文章后决定自己做一个自定义的,实现了效果,代码如下:
UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"有新版本发布啦!"message:messagedelegate:selfcancelButtonTitle:@"忽略"otherButtonTitles:@"前往更新",nil];
//
alertView.tag=1243;
//如果你的系统大于等于6.0
if(floor(NSFoundationVersionNumber) >NSFoundationVersionNumber_iOS_6_1)
{
//计算文字的尺寸大小
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的设置方法
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;