日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UIAlertController 简单修改title以及按钮的字体颜色

發(fā)布時間:2025/7/25 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UIAlertController 简单修改title以及按钮的字体颜色 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

https://www.jianshu.com/p/cecd1b4bbf27

歡迎加入 iOS開發(fā)QQ群:151133690

實現(xiàn)此效果的另一種簡單調(diào)用方式已經(jīng)上傳到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

來源:簡書

簡書著作權歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權并注明出處。

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/sundaysgarden/p/10309546.html

總結(jié)

以上是生活随笔為你收集整理的UIAlertController 简单修改title以及按钮的字体颜色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。