iOS開發之iOS6.0\iOS7.0\iOS8.0的UIAlertView message 文字對齊設定

來源:互聯網
上載者:User

標籤:

            是不是發現原來這段代碼:

#pragma mark -

#pragma mark - alert delegate

- (void) willPresentAlertView:(UIAlertView *)alertView

{

   for (UIView *subViewin alertView.subviews)

    {

       UILabel *tmpLabel = (UILabel *)subView;

        tmpLabel.textAlignment =NSTextAlignmentLeft;

    }

}

在iOS7.0及以上版本不能用(說明蘋果對私人api管理越來越嚴格,猜測),如果還想uialertview文字對其要費一些心思了。

 

如果你有這樣的需求:

1>message 資訊顯示居靠左對齊,如(iOS6.0和iOS7.1顯示)

 

         

 

2>在iOS7.0以下版本標題置中,message居左(如)。

 

我們對上述代理方法稍作更改如下:

 

#pragma mark -

#pragma mark - alert delegate

- (void) willPresentAlertView:(UIAlertView *)alertView

{

    //由於不希望標題也居左

   NSInteger labelIndex = 1;

    //在ios7.0一下版本這個方法是可以的

   for (UIView *subViewin alertView.subviews)

    {

        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)

        {

           if ([subView isKindOfClass: [UILabelclass]])

            {

               if (labelIndex > 1)

                {

                   UILabel *tmpLabel = (UILabel *)subView;

                    tmpLabel.textAlignment =NSTextAlignmentLeft;

                }

               //過濾掉標題

                labelIndex ++;

            }

        }

    }

}

但這隻能在ios7.0以下版本可以生效;如果是8.0,處理方式還不一樣,具體如下:

 

 

- (void) showAlertWithMessage:(NSString *) message

{

    //8.0

    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"ffff"message:message preferredStyle:UIAlertControllerStyleAlert];

        

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

        //paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

        paragraphStyle.alignment = NSTextAlignmentLeft;

        //行間距

        paragraphStyle.lineSpacing = 5.0;

        

        NSDictionary * attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:18.0], NSParagraphStyleAttributeName : paragraphStyle};

        NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:message];

        [attributedTitle addAttributes:attributes range:NSMakeRange(0, message.length)];

        [alertController setValue:attributedTitle forKey:@"attributedMessage"];//attributedTitle\attributedMessage

        //end ---

        

        

        UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"cancel"

                                                                style: UIAlertActionStyleDefault

                                                              handler:^(UIAlertAction *action) {

                                                                  UITextField *textField = alertController.textFields[0];

                                                                  NSLog(@"text was %@", textField.text);

                                                              }];

        UIAlertAction *defaultAction2 = [UIAlertAction actionWithTitle:@"ok"

                                                                style: UIAlertActionStyleDefault

                                                              handler:^(UIAlertAction *action) {

                                                                  NSLog(@"ok btn");

                                                                  

                                                                  [alertControllerdismissViewControllerAnimated:YES completion:nil];

 

                                                              }];

 

        [alertController addAction:defaultAction1];

        [alertController addAction:defaultAction2];

        //添加textfield

 

        UIViewController *rootViewController = [UIApplicationsharedApplication].keyWindow.rootViewController;

        [rootViewController presentViewController:alertController animated: YES completion: nil];

 

    }else{

        

        UIAlertView *tmpAlertView = [[UIAlertView alloc] initWithTitle:@"測試換行"

                                                               message:message

                                                              delegate:self

                                                     cancelButtonTitle:nil

                                                     otherButtonTitles:@"知道了", nil];

        

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

         

        if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)

        {

            CGSize size = [self.messageString sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(240, 1000) lineBreakMode:NSLineBreakByTruncatingTail];

            

            UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, size.height)];

            textLabel.font = [UIFont systemFontOfSize:15];

            textLabel.textColor = [UIColor blackColor];

            textLabel.backgroundColor = [UIColor clearColor];

            textLabel.lineBreakMode = NSLineBreakByWordWrapping;

            textLabel.numberOfLines = 0;

            textLabel.textAlignment = NSTextAlignmentLeft;

            textLabel.text = self.messageString;

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

            

            //這個地方別忘了把alertview的message設為空白

            tmpAlertView.message = @"";

            

        }

        

        [tmpAlertView show];

    }

    

}

 

這樣的話就可以了。

 

調用這個方法:

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorgrayColor];

    

    self.messageString [email protected]"1.第一行我是3個子\n2.第二行我是好幾個字反正目的是為了和第一行區分開來\n3.哈哈我是陪襯的";

    

   UIButton *alertBtn = [[UIButtonalloc] initWithFrame:CGRectMake(0,200, 320, 40)];

    [alertBtn setTitle:@"點我啊,我會alert" forState:UIControlStateNormal];

    alertBtn.backgroundColor = [UIColorredColor];

    [alertBtn addTarget:selfaction:@selector(alertBtnTapped)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:alertBtn];

 

}

這裡別忘聲明一個屬性self.messageString

 

iOS開發之iOS6.0\iOS7.0\iOS8.0的UIAlertView message 文字對齊設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.