OC:修改UIAlertController文本样式

效果图

说明:如效果图里的内容所示,该方法可以用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:@"装逼结束" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

扩展

需求:假如有一个数组里面放着多个Person对象,Person对象有age和height两个属性,我们需要根据Person的age对数组进行升序排列;如果age相同,则按照height升序排列。如:

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

实现代码:

#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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容