UIAlertView上的文字居左顯示

最近做了一個提示版本更新的功能,發現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:message delegate:selfcancelButtonTitle:@"忽略"otherButtonTitles:@"前往更新",nil];

                    //                

                    alertView.tag=1243;

                    //如果你的系統大於等於6.0

                    if (floor(NSFoundationVersionNumber) >NSFoundationVersionNumber_iOS_6_1)

                    {

//計算文字的尺寸大小

                        CGSize size = [message sizeWithFont:[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上

                        [alertView setValue:textLabel forKey:@"accessoryView"];

                        

                        //清空alertview上的message

                        alertView.message =@"";

                    }

                    

                    [alertView show];

附帶  UIalertController 的文字居左:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];

                     //如果你的系統大於等於7.0

                     UIView *subView1 = alert.view.subviews[0];

                     UIView *subView2 = subView1.subviews[0];

                     UIView *subView3 = subView2.subviews[0];

                     UIView *subView4 = subView3.subviews[0];

                     UIView *subView5 = subView4.subviews[0];

                     UILabel * messageLabel = subView5.subviews[1];

                     messageLabel.textAlignment=NSTextAlignmentLeft;

                     messageLabel.text=message;

                     [alert addAction:[UIAlertAction actionWithTitle:@"前往更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                         NSString * url = [GlobalData VersionUrl];

                         [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];

                         

                     }]];

                     

                     [alert addAction:[UIAlertAction actionWithTitle:@"忽略" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

                         [alert dismissViewControllerAnimated:YES completion:nil];

                     }]];

                     

[strongSelf presentViewController:alert animated:YES completion:nil];





Demo下載地址:http://download.csdn.net/detail/bddzzw/9599669

發佈了73 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章