fonts - 我可以在iOS7中更改datePicker的字体颜色吗?

标签 fonts datepicker ios7 xcode5

刚刚下载了我的 xcode 5 副本,想知道是否有人知道如何更改日期选择器中字体的颜色或大小?

最佳答案

我的应用程序需要类似的东西,但最终还是走了很长一段路。遗憾的是,没有更简单的方法可以简单地切换到 UIDatePicker 的白色文本版本。

当 setTextColor: 消息发送到标签时,下面的代码使用 UILabel 上的类别强制标签的文本颜色为白色。为了不对应用程序中的每个标签执行此操作,我将其过滤为仅在它是 UIDatePicker 类的 subview 时才应用。最后,一些标签在添加到其 super View 之前已设置颜色。为了捕获这些,代码重写 willMoveToSuperview: 方法。

最好将以下内容分为多个类别,但为了便于发布,我已将其全部添加到此处。

#import "UILabel+WhiteUIDatePickerLabels.h"
#import <objc/runtime.h>

@implementation UILabel (WhiteUIDatePickerLabels)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceSelector:@selector(setTextColor:)
                      withNewSelector:@selector(swizzledSetTextColor:)];
        [self swizzleInstanceSelector:@selector(willMoveToSuperview:)
                      withNewSelector:@selector(swizzledWillMoveToSuperview:)];
    });
}

// Forces the text colour of the label to be white only for UIDatePicker and its components
-(void) swizzledSetTextColor:(UIColor *)textColor {
    if([self view:self hasSuperviewOfClass:[UIDatePicker class]] ||
       [self view:self hasSuperviewOfClass:NSClassFromString(@"UIDatePickerWeekMonthDayView")] ||
       [self view:self hasSuperviewOfClass:NSClassFromString(@"UIDatePickerContentView")]){
        [self swizzledSetTextColor:[UIColor whiteColor]];
    } else {
        //Carry on with the default
        [self swizzledSetTextColor:textColor];
    }
}

// Some of the UILabels haven't been added to a superview yet so listen for when they do.
- (void) swizzledWillMoveToSuperview:(UIView *)newSuperview {
    [self swizzledSetTextColor:self.textColor];
    [self swizzledWillMoveToSuperview:newSuperview];
}

// -- helpers --
- (BOOL) view:(UIView *) view hasSuperviewOfClass:(Class) class {
    if(view.superview){
        if ([view.superview isKindOfClass:class]){
            return true;
        }
        return [self view:view.superview hasSuperviewOfClass:class];
    }
    return false;
}

+ (void) swizzleInstanceSelector:(SEL)originalSelector
                 withNewSelector:(SEL)newSelector
{
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method newMethod = class_getInstanceMethod(self, newSelector);

    BOOL methodAdded = class_addMethod([self class],
                                       originalSelector,
                                       method_getImplementation(newMethod),
                                       method_getTypeEncoding(newMethod));

    if (methodAdded) {
        class_replaceMethod([self class],
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

@end

关于fonts - 我可以在iOS7中更改datePicker的字体颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807940/

相关文章:

iphone - iOS字体:如何在iOS中显示科普特字体

php - 如何允许单个 Wordpress 站点接受我的 .ttf 文件上传?

python - 如何获取(网络)字体中单个字符的大小?

JQueryMobile Datepicker 和新框架

ios - 如何在队列外使用 JsonArray

c# - PDFsharp - 使用私有(private)字体

datepicker - Yii2 Kartik DatePicker - 日期不应超过当前日期

javascript - 以编程方式更改 jquery ui datepicker

ios - UINavigationItem 提示问题

ios - NSURLCache 不在 iOS 7 中存储缓存数据