OC:修改UIAlertController文本樣式

效果圖

說明:如效果圖里的內(nèi)容所示,該方法可以用attributedTitle修改Title樣式,用attributedMessage修改Msg樣式,比如文本顏色/字體大小和對其方式/行間距等。

- (void)showAlertVC {
    
    NSString *message = @"改Title樣式用`attributedTitle`;改Msg樣式用`attributedMessage`,比如文本顏色/字體大小和對其方式/行間距";
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:message];
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:13],
                                 NSForegroundColorAttributeName: [UIColor redColor],
                                 NSParagraphStyleAttributeName : paragraphStyle};
    [attributedTitle addAttributes:attributes range:NSMakeRange(0, message.length)];
    [alertVC setValue:attributedTitle forKey:@"attributedMessage"];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"裝逼結(jié)束" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

擴(kuò)展

需求:假如有一個(gè)數(shù)組里面放著多個(gè)Person對象,Person對象有age和height兩個(gè)屬性,我們需要根據(jù)Person的age對數(shù)組進(jìn)行升序排列;如果age相同,則按照height升序排列。如:

age(年齡遞增排序) height(年齡相同,就身高遞增排序)
7 174
8 154
8 156
8 171
9 145

實(shí)現(xiàn)代碼:

#import "ViewController.h"

@interface Person : NSObject
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, assign) CGFloat height;

- (instancetype)initWithAge:(NSInteger)age height:(CGFloat)height;

@end

@implementation Person

- (instancetype)initWithAge:(NSInteger)age height:(CGFloat)height {
    if (self = [super init]) {
        self.age = age;
        self.height = height;
    }
    return self;
}

@end

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)checkSOSAccessable {
    
    NSArray *array = @[
        [[Person alloc] initWithAge:11 height:22],
        [[Person alloc] initWithAge:4 height:33],
        [[Person alloc] initWithAge:24 height:26],
        [[Person alloc] initWithAge:8 height:90],
        [[Person alloc] initWithAge:11 height:20],
        [[Person alloc] initWithAge:11 height:24],
        [[Person alloc] initWithAge:11 height:22],
    ];
    
    for (Person *person in array) {
        NSLog(@"%ld---%.0f", person.age, person.height);
    }
    NSLog(@"======================");
    
    NSSortDescriptor *descripor1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
    NSSortDescriptor *descripor2 = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:YES];
    array = [array sortedArrayUsingDescriptors:@[descripor1, descripor2]];
    
    for (Person *person in array) {
        NSLog(@"%ld---%.0f", person.age, person.height);
    }
}

@end

打印

EmergencySOS[46129:817777] 11---22
EmergencySOS[46129:817777] 4---33
EmergencySOS[46129:817777] 24---26
EmergencySOS[46129:817777] 8---90
EmergencySOS[46129:817777] 11---20
EmergencySOS[46129:817777] 11---24
EmergencySOS[46129:817777] 11---22
EmergencySOS[46129:817777] =============
EmergencySOS[46129:817777] 4---33
EmergencySOS[46129:817777] 8---90
EmergencySOS[46129:817777] 11---20
EmergencySOS[46129:817777] 11---22
EmergencySOS[46129:817777] 11---22
EmergencySOS[46129:817777] 11---24
EmergencySOS[46129:817777] 24---26
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容