歡迎加入 iOS開發(fā)QQ群:151133690
本篇內(nèi)容是對<a href ="http://www.reibang.com/p/cecd1b4bbf27" > UIAlertController 簡單修改title以及按鈕的字體顏色 </a>的加強(qiáng)版,對系統(tǒng)UIAlertController類添加Category使用更加簡單,同時添加block回調(diào)方法
/**
根據(jù)otherActionTitles數(shù)組創(chuàng)建UIAlertController
@param title title
@param message message
@param cancelActionTitle 取消Action標(biāo)題
@param otherActionTitles 其他Action標(biāo)題
@param handle 點(diǎn)擊回調(diào) -1 取消Action點(diǎn)擊 0~n 其他Action點(diǎn)擊
@return UIAlertController
*/
+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray<NSString *> *)otherActionTitles handle:(void (^)(NSInteger index))handle;
先來看看效果示例
做法很簡單,自己寫UIAlertController的Category 別忘了導(dǎo)入頭文件,然后復(fù)制如下代碼到.h 和.m即可.
UIAlertController+Color.h
//
// UIAlertController+Color.h
// UIAlertControllerColor
//
// Created by benben on 16/12/15.
// Copyright ? 2016年 benben. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIAlertController (Color)
@property (nonatomic,strong) UIColor *tintColor; /**< 統(tǒng)一按鈕樣式 不寫系統(tǒng)默認(rèn)的藍(lán)色 */
@property (nonatomic,strong) UIColor *titleColor; /**< 標(biāo)題的顏色 */
@property (nonatomic,strong) UIColor *messageColor; /**< 信息的顏色 */
/**
根據(jù)otherActionTitles數(shù)組創(chuàng)建UIAlertController
@param title title
@param message message
@param cancelActionTitle 取消Action標(biāo)題
@param otherActionTitles 其他Action標(biāo)題
@param handle 點(diǎn)擊回調(diào) -1 取消Action點(diǎn)擊 0~n 其他Action點(diǎn)擊
@return UIAlertController
*/
+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray<NSString *> *)otherActionTitles handle:(void (^)(NSInteger index))handle;
@end
@interface UIAlertAction (Color)
@property (nonatomic,strong) UIColor *textColor; /**< 按鈕title字體顏色 */
@end
UIAlertController+Color.m
//
// UIAlertController+Color.h
// UIAlertControllerColor
//
// Created by benben on 16/12/15.
// Copyright ? 2016年 benben. All rights reserved.
//
#import "UIAlertController+Color.h"
#import <objc/runtime.h>
@implementation UIAlertController (Color)
/**
根據(jù)otherActionTitles數(shù)組創(chuàng)建UIAlertController
@param title title
@param message message
@param cancelActionTitle 取消Action標(biāo)題
@param otherActionTitles 其他Action標(biāo)題
@param handle 點(diǎn)擊回調(diào) -1 取消Action點(diǎn)擊 0~n 其他Action點(diǎn)擊
@return UIAlertController
*/
+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray<NSString *> *)otherActionTitles handle:(void (^)(NSInteger))handle
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
//如果有取消按鈕
if (cancelActionTitle) {
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelActionTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (handle) {
handle(-1);
}
}];
[alertController addAction:cancelAction];
}
if (otherActionTitles.count > 0) {
[otherActionTitles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (handle) {
handle(idx);
}
}];
[alertController addAction:otherAction];
}];
}
return alertController;
}
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
//按鈕統(tǒng)一顏色
if (self.tintColor) {
for (UIAlertAction *action in self.actions) {
if (!action.textColor || action.style != UIAlertActionStyleDestructive) {
action.textColor = self.tintColor;
}
}
}
}
-(UIColor *)tintColor
{
return objc_getAssociatedObject(self, @selector(tintColor));
}
-(void)setTintColor:(UIColor *)tintColor
{
objc_setAssociatedObject(self, @selector(tintColor), tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIColor *)titleColor
{
return objc_getAssociatedObject(self, @selector(titleColor));
}
-(void)setTitleColor:(UIColor *)titleColor
{
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
for(int i = 0;i < count;i ++){
Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
//標(biāo)題顏色
if ([ivarName isEqualToString:@"_attributedTitle"] && self.title && titleColor) {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0]}];
[self setValue:attr forKey:@"attributedTitle"];
}
}
free(ivars);
objc_setAssociatedObject(self, @selector(titleColor), titleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIColor *)messageColor
{
return objc_getAssociatedObject(self, @selector(messageColor));
}
-(void)setMessageColor:(UIColor *)messageColor
{
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
for(int i = 0;i < count;i ++){
Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
//描述顏色
if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && messageColor) {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:messageColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
[self setValue:attr forKey:@"attributedMessage"];
}
}
free(ivars);
objc_setAssociatedObject(self, @selector(messageColor), messageColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@implementation UIAlertAction (Color)
-(UIColor *)textColor
{
return objc_getAssociatedObject(self, @selector(textColor));
}
//按鈕標(biāo)題的字體顏色
-(void)setTextColor:(UIColor *)textColor
{
if (self.style == UIAlertActionStyleDestructive) {
return;
}
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
for(int i =0;i < count;i ++){
Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
if ([ivarName isEqualToString:@"_titleTextColor"]) {
[self setValue:textColor forKey:@"titleTextColor"];
}
}
free(ivars);
objc_setAssociatedObject(self, @selector(textColor), textColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
用法就很簡單了,和系統(tǒng)原生UIAlertController一樣
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你確定退出嗎" message:@"真的要退出嗎?" preferredStyle:UIAlertControllerStyleActionSheet];
alertController.titleColor = [UIColor redColor];//修改title的顏色
alertController.messageColor = [UIColor yellowColor]; //修改message的顏色
alertController.tintColor = [UIColor redColor]; //全局修改Action的顏色 -- 當(dāng)然你也可以單獨(dú)修改某一個Action的顏色 看下面??
//添加返回按鈕
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancel];
cancel.textColor = [UIColor redColor]; //單個修改Action的顏色
//添加確定退出按鈕
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:alertAction];
[self presentViewController:alertController animated:YES completion:nil];
如果你比較懶 那么我推薦你這么使用
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"我是標(biāo)題" message:@"我是message" cancelActionTitle:@"我是取消按鈕" otherActionTitles:@[@"title1",@"title2",@"title3"] handle:^(NSInteger index) {
//Action點(diǎn)擊回調(diào)
//注意 cancelAction index 為 -1,
// otherAction index為 otherActionTitles數(shù)組下標(biāo)
}];
alertController.titleColor = [UIColor redColor];//修改title的顏色
alertController.messageColor = [UIColor yellowColor]; //修改message的顏色
alertController.tintColor = [UIColor redColor]; //全局修改Action的顏色
//注意 該方法 不能單獨(dú)設(shè)置 某一個Action的顏色 只能修改全部
alertController.tintColor = [UIColor redColor];
[self presentViewController:alertController animated:YES completion:nil];
好了 ,就這些了...