运行时机制修改UIAlertController文字对齐方式


#import "ViewController.h"

#import <objc/runtime.h>

@interface ViewController ()

{

    UIAlertController *_alert;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)showAlertViewWithChange:(BOOL)isChange {

    

    _alert = [UIAlertController alertControllerWithTitle:@"title"

                                                 message:@"this is a ale史蒂夫i 哦额就让我颇具惹我哭了认可了嗯弗兰克;摩免费rt"

                                          preferredStyle:UIAlertControllerStyleAlert];

    

    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"default"

                                                            style:UIAlertActionStyleDefault

                                                          handler:^(UIAlertAction * _Nonnull action) {

                                                              

                                                          }];

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

                                                           style:UIAlertActionStyleCancel

                                                         handler:^(UIAlertAction * _Nonnull action) {

                                                             

                                                         }];

    UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"destructive"

                                                                style:UIAlertActionStyleDestructive

                                                              handler:^(UIAlertAction * _Nonnull action) {

                                                                  

                                                              }];

    

    [_alert addAction:defaultAction];

    [_alert addAction:cancelAction];

    [_alert addAction:destructiveAction];

    

//    if (isChange == YES) {

      [self runtimeProperty];

//    }


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

}


- (IBAction)presentOriginalTap:(id)sender {

    [self runtimeProperty];

    [self showAlertViewWithChange:NO];

   

}


- (IBAction)prensetCustomTap:(id)sender {

    

//        [self runtimeMethod];

    [self runtimeProperty];

    [self showAlertViewWithChange:YES];

}


/**

 *  获取UIAlertController方法名

 */

- (void)runtimeMethod{

    unsigned int count = 0;

    Method *alertMethod = class_copyMethodList([UIAlertController class], &count);

    for (int i = 0; i < count; i++) {

        SEL name = method_getName(alertMethod[i]);

        NSString *methodName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];

        NSLog(@"methodName ============= %@",methodName);

    }

}



/**

 *  获取UIAlertController属性名

 */

- (void)runtimeProperty {

    unsigned int count = 0;

    Ivar *property = class_copyIvarList([UIAlertController class], &count);

    for (int i = 0; i < count; i++) {

        Ivar var = property[i];

        const char *name = ivar_getName(var);

        const char *type = ivar_getTypeEncoding(var);

        NSLog(@"%s =====ppppppp========== %s",name,type);

    }


    Ivar message = property[2];

    

/**

 *  字体修改

 */

    UIFont *big = [UIFont systemFontOfSize:25];

    UIFont *small = [UIFont systemFontOfSize:18];

    UIColor *red = [UIColor redColor];

    UIColor *blue = [UIColor blueColor];

    

    NSTextAlignment*ali=NSTextAlignmentLeft;

    

    

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"this is a ale史蒂夫i 哦额就让我颇具惹我哭了认可了嗯弗兰克;摩免费rt" attributes:@{NSFontAttributeName:big,

NSForegroundColorAttributeName:red,}];

    

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

    paragraph.alignment = NSTextAlignmentLeft;//设置对齐方式

    

    [str setAttributes:@{NSFontAttributeName:small} range:NSMakeRange(0, 2)];

    [str setAttributes:@{NSForegroundColorAttributeName:blue} range:NSMakeRange(0, 4)];

    [str setAttributes:@{NSParagraphStyleAttributeName:paragraph} range:NSMakeRange(0, 4)];

//    [str setAttributedString:<#(nonnull NSAttributedString *)#>];

//最后把message内容替换掉

    object_setIvar(_alert, message, str);

//    object_setIvar(_alert, message, paragraph);

}



@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值