Just My Life & My Work

[iOS] UIAlertController 字體靠左

原生的UIAlertController字體靠中,看到高人hack出如何改變字體特性,終於豁然開朗呢!

uialertcontroller-original

知道之後,自己學著如何hack吧!

/**
 Theme: UIAlertController Text Alignment
 IDE: Xcode 8
 Language: Objective C
 Date: 105/12/14
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"確定要兌換嗎?"
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {

                                                         }];
    [alert addAction:cancelAction];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                     }];
    [alert addAction:okAction];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;

    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    [alert setValue:attributedStr forKey:@"attributedMessage"];

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

這裡的重點是這個key:attributedMessage

uialertcontroller-modified

參考:UIAlertController text alignment

廣告

隨意留個言吧:)~

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

標籤雲

%d 位部落客按了讚: