UI基礎(chǔ)_UIAlertController/UIAlertView使用和字體大小顏色

1.UIAlertView在iOS9.0后被拋棄

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"請(qǐng)核對(duì)后重新輸入",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"知道了",nil) otherButtonTitles:nil, nil];
    [alertView show];

2.UIAlertController

NSString *message = NSLocalizedString(@"請(qǐng)核對(duì)后重新輸入",nil);
    NSString *title = @"提示";
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    //改變title的大小和顏色
    NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
    [titleAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, title.length)];
    [titleAtt addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, title.length)];
    [alertController setValue:titleAtt forKey:@"attributedTitle"];
    //改變message的大小和顏色
    NSMutableAttributedString *messageAtt = [[NSMutableAttributedString alloc] initWithString:message];
    [messageAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, message.length)];
    [messageAtt addAttribute:NSForegroundColorAttributeName value:[UIColor darkTextColor] range:NSMakeRange(0, message.length)];
    [alertController setValue:messageAtt forKey:@"attributedMessage"];
    
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"知道了",nil) style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:alertAction];
    [self presentViewController:alertController animated:YES completion:nil];

注:當(dāng)title設(shè)置為nil時(shí),message的字體大小會(huì)是標(biāo)題的大小,如果單純只是希望message小些,可設(shè)置title = @ "" 即可
注:UIAlertAction的字體也可改變,打印屬性即可查看。取消按鈕字體顏色的key是@"titleTextColor"

3.屬性可通過runtime打印

unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
    for (int i = 0; i<count; i++) {
        Ivar ivar = ivars[i];
        NSLog(@"%s------%s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容