在我們app的開發(fā)過程經(jīng)常會遇到各種彈框歧譬,提示框的需求,然而系統(tǒng)的自帶的字體顏色是如此的ugly麻昼。那我們今天就討論一下娜氏,在ios7.0和8.0上面如何修改彈框的的title的字體顏色。
- 首先在iOS7.0修改UIActionSheet title的字體是很簡單的覆获,設置代理马澈,在willPresentActionSheet方法中修改。代碼如下:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *subViwe in actionSheet.subviews) {
if ([subViwe isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *)subViwe;
label.font = [UIFont systemFontOfSize:16];
label.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMinY(label.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame)+20);
}
if ([subViwe isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton*)subViwe;
if ([button.titleLabel.text isEqualToString:@"確定"]) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
button.titleLabel.font = [UIFont systemFontOfSize:18];
}
}
}
當然自從xcode在系統(tǒng)8.0采用了UIAlertController過后弄息,7.0的方法就沒有用了痊班,那么在iOS8.0過后我們怎么修改title的字體顏色呢?在這里推薦一款很好用的第三方里脊串的MMPopupView 里面的控件我覺得還是很好用的摹量!那我就不想用第三方涤伐,只想在系統(tǒng)方法上修改怎么辦呢。很簡單缨称,下面貼上我的代碼凝果!也就幾段代碼,我就不上傳我的github了睦尽。
- iOS8.0 系統(tǒng)UIAlertController創(chuàng)建actionSheet的方法
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: @"USD($)" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]];
[alertController addAction: [UIAlertAction actionWithTitle:@"RMB(¥)" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
[self presentViewController: alertController animated: YES completion: nil];
- 首先通過runtime獲取對應的內部屬性
包含頭文件#import <objc/runtime.h>
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
for (int i = 0; i<count; i++) {
// 取出成員變量
// Ivar ivar = *(ivars + i);
Ivar ivar = ivars[i];
// 打印成員變量名字
NSLog(@"%s------%s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
}
-
獲取到對應的屬性我們就可以修改系統(tǒng)內部的默認值了
// 取消按鈕 -(void)addCancelActionTarget:(UIAlertController*)alertController title:(NSString *)title
{
UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[action setValue:[UIColor purpleColor] forKey:@"_titleTextColor"];
[alertController addAction:action];
}
//添加對應的title 這個方法也可以傳進一個數(shù)組的titles 我只傳一個是為了方便實現(xiàn)每個title的對應的響應事件不同的需求不同的方法
- (void)addActionTarget:(UIAlertController *)alertController title:(NSString *)title color:(UIColor *)color action:(void(^)(UIAlertAction *action))actionTarget
{
UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
actionTarget(action);
}];
[action setValue:color forKey:@"_titleTextColor"];
[alertController addAction:action];
}
- 最后具體的實現(xiàn)代碼就是這樣的 大家可以復制代碼自己去試試
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[self addActionTarget:alert title:@"星期一" color: [UIColor redColor] action:^(UIAlertAction *action) {
NSLog(@"nicaicai");
}];
[self addActionTarget:alert title:@"星期二" color: [UIColor redColor] action:^(UIAlertAction *action) {
NSLog(@"nicaicai");
}];
[self addActionTarget:alert title:@"星期三" color: [UIColor redColor] action:^(UIAlertAction *action) {
NSLog(@"nicaicai");
}];
[self addActionTarget:alert title:@"星期四" color: [UIColor redColor] action:^(UIAlertAction *action) {
NSLog(@"nicaicai");
}];
[self addCancelActionTarget:alert title:@"取消"];
[self presentViewController:alert animated:YES completion:nil];
##實現(xiàn)效果如下
![F9D4E954-DE0F-4624-B976-C9FB8BD86505.png](http://upload-images.jianshu.io/upload_images/1292550-281c9cd7ac5cabf9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)