UIAlertController 简单修改title以及按钮的字体颜色
https://www.jianshu.com/p/cecd1b4bbf27
歡迎加入 iOS開發(fā)QQ群:151133690
實現(xiàn)此效果的另一種簡單調用方式已經上傳到gitHub,請移步https://github.com/benben-hello/BBAlertController
先來幾張效果圖吧:
?
?
?
苦逼的開發(fā)者,最終敗給了一個任性的UI,系統(tǒng)原生UIAlertController的按紐顏色必須改.于是,開始了不歸路.
之前的版本是自己用view寫的一個仿系統(tǒng)UIActionSheet,動畫感覺都挺好,就是毛玻璃背景沒有系統(tǒng)的好,由于最低兼容了ios8,所以就拋棄了UIActionSheet,改用UIAlertController.
做法其實很簡單,采用runtime機制.對于runtime不了解的,我想還是別看各種介紹文章了,找一個簡單的demo多寫幾遍,就行了.
做法很簡單,自己寫一個類 繼承自UIAlertController,還是先把.h和.m的代碼都給大家吧.
SCAlertController.h
//
//? SCAlertController.h
//? SCAlertController
//
//? Created by it3部01 on 16/8/3.
//? Copyright ? 2016年 benben. All rights reserved.
//
?
#import <UIKit/UIKit.h>
?
@interface SCAlertAction : UIAlertAction
?
@property (nonatomic,strong) UIColor *textColor; /**< 按鈕title字體顏色 */
?
@end
?
@interface SCAlertController : UIAlertController
?
@property (nonatomic,strong) UIColor *tintColor; /**< 統(tǒng)一按鈕樣式 不寫系統(tǒng)默認的藍色 */
@property (nonatomic,strong) UIColor *titleColor; /**< 標題的顏色 */
@property (nonatomic,strong) UIColor *messageColor; /**< 信息的顏色 */
?
@end
?
SCAlertController.m
//
//? SCAlertController.m
//? SCAlertController
//
//? Created by it3部01 on 16/8/3.
//? Copyright ? 2016年 benben. All rights reserved.
//
?
#import "SCAlertController.h"
#import <objc/runtime.h>
?
@implementation SCAlertAction
?
//按鈕標題的字體顏色
-(void)setTextColor:(UIColor *)textColor
{
? ? _textColor = textColor;
?? ?
? ? 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"];
? ? ? ? }
? ? }
}
?
@end
?
?
@implementation SCAlertController
?
-(void)viewDidLoad
{
? ? [super viewDidLoad];
?? ?
? ? 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:@"_attributedTitle"] && self.title && self.titleColor) {
?
? ? ? ? ? ? NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:self.titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0]}];
? ? ? ? ? ? [self setValue:attr forKey:@"attributedTitle"];
? ? ? ? }
?? ? ? ?
? ? ? ? //描述顏色
? ? ? ? if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && self.messageColor) {
?? ? ? ? ? ?
? ? ? ? ? ? NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:self.messageColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
? ? ? ? ? ? [self setValue:attr forKey:@"attributedMessage"];
? ? ? ? }
? ? }
?? ?
? ? //按鈕統(tǒng)一顏色
? ? if (self.tintColor) {
? ? ? ? for (SCAlertAction *action in self.actions) {
? ? ? ? ? ? if (!action.textColor) {
? ? ? ? ? ? ? ? action.textColor = self.tintColor;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
@end
?
一般來說對于SCAlertController里面的屬性應該像SCAlertAction一樣放在setter方法里面修改,這里我表示為了方便就放在這個方法里面了.
-(void)viewDidLoad
{
? ? [super viewDidLoad];? ?
}
?
用法就很簡單了,和系統(tǒng)原生UIAlertController一樣,只是把UI換成SC,當然你可以改成自己喜歡的,但是別忘了改完.
SCAlertController *alertController = [SCAlertController alertControllerWithTitle:@"你確定要退出嗎?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
? ? ? ? alertController.tintColor = [UIColor redColor]; //這里統(tǒng)一設置各個按鈕的顏色都為紅色.
當然,你還可以自定義某一個按鈕的顏色.比如下面的取消按鈕
? ? ? ? alertController.titleColor = [UIColor redColor];
?? ? ? ?
? ? ? ? //取消
? ? ? ? SCAlertAction *cancelAction = [SCAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil];
? ? ? ?
? ? ? ? //單獨修改一個按鈕的顏色
? ? ? ? cancelAction.textColor = [UIColor greenColor];
? ? ? ? [alertController addAction:cancelAction];
?? ? ? ?
? ? ? ? //退出
? ? ? ? SCAlertAction *exitAction = [SCAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? ? ?
? ? ? ? }];
? ? ? ? [alertController addAction:exitAction];
?? ? ? ?
? ? ? ? [self presentViewController:alertController animated:YES completion:nil];
? ? }
?
作者:青春微涼來時路
鏈接:https://www.jianshu.com/p/cecd1b4bbf27
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯(lián)系作者獲得授權并注明出處。
?
?
?
?
?
轉載于:https://www.cnblogs.com/sundaysgarden/p/10309546.html
總結
以上是生活随笔為你收集整理的UIAlertController 简单修改title以及按钮的字体颜色的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: k8s(1)-使用kubeadm安装Ku
- 下一篇: 犹豫了许久,还是写个年总结记录一下吧