刘的个人专题

UIAlertController创建修改title与messa

2018-03-09  本文已影响2人  辉546

UIAlertController创建修改title与message字体和颜色以符合要求,且在子视图(如cell)中展示alert;

/**

    变更佩戴的守护微章

*/

- (void)changeGuardianMicoSeal{

    //    //修改title

    //    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"物品详情"];

    //    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];

    //    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];

    //    [alertController setValue:alertControllerStr forKey:@"attributedTitle"];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定佩戴展示我是牛泡泡的初级微章么" preferredStyle:UIAlertControllerStyleAlert];

    // 2.创建并添加按钮

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"OK Action");

    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"Cancel Action");

    }];

    [alertController addAction:okAction]; // A

    [alertController addAction:cancelAction]; // B

    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"确定佩戴展示我是牛泡泡的初级微章么"];

    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 5)];

    [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];

    [[self topViewController] presentViewController:alertController animated:YES completion:nil];

}

- (UIViewController *)topViewController {

    UIViewController *resultVC;

    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];

    while (resultVC.presentedViewController) {

        resultVC = [self _topViewController:resultVC.presentedViewController];

    }

    return resultVC;

}

- (UIViewController *)_topViewController:(UIViewController *)vc {

    if ([vc isKindOfClass:[UINavigationController class]]) {

        return [self _topViewController:[(UINavigationController *)vc topViewController]];

    } else if ([vc isKindOfClass:[UITabBarController class]]) {

        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];

    } else {

        return vc;

    }

    return nil;

}

上一篇 下一篇

猜你喜欢

热点阅读