为 iOS7 自定义 UIDatePicker 的文本颜色(就像邮箱一样) [英] Customize text color of UIDatePicker for iOS7 (just like Mailbox does)

查看:12
本文介绍了为 iOS7 自定义 UIDatePicker 的文本颜色(就像邮箱一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了最令人沮丧的两难境地.我上下研究过,可以清楚地看到 Apple 不希望我们篡改 iOS 7.好吧,我想篡改.而且,Mailbox 的团队清楚地知道如何去做并获得批准.

I'm having the most frustrating dilemma. I've researched up and down and can clearly see that Apple does not want us tampering with iOS 7. Well, I want to tamper. And, the team at Mailbox clearly figured out how to do it and get approved.

我想要实现的主要目标是将标签颜色更改为白色.

The main thing that I'm trying to achieve is to change the label color to white.

我的第一个想法是他们正在使用自定义 UIPickerView,它只是模仿 UIDatePicker,但我不认为是这种情况.

My first thought was they are using a custom UIPickerView that just mimics a UIDatePicker, but I just don't think this is the case.

我放大了一个小片段,发现了一个正常的 UIDatePicker(黑线)的残留物以及字母W"上的剪裁.

I zoomed in on a small fragment and discovered remnants of a normal UIDatePicker (black lines) along with clipping on the letter "W".

现在我已经高高在上.做了一些运行时黑客,弄乱了 UIAppearance,甚至挖掘了一些私有 API 只是为了看看这是否可能.

Now I've scoured high and low. Did some runtime hacking, messed with UIAppearance, and even dug into some private APIs just to see if this is possible.

我已经接近了,非常接近,但它使用了一个私有 API,如果你滚动得足够快,标签会再次变黑.

I got close, very close, but it used a private API, and if you scrolled fast enough the labels would turn black again.

我完全不知道如何在不违反规则或 b) 花费无数小时重新实现 UIDatePicker 的情况下执行此操作.

I'm completely at a loss on how to do this without a) breaking the rules or b) spending countless hours reimplementing UIDatePicker.

邮箱,告诉我你的秘密!如果其他人有任何建议(我的意思是任何建议),请告诉我.

Mailbox, tell me your secrets! And if anyone else has any suggestions (and I mean any), please let me know.

另外,这是我得到的最接近的:

Also, this is the closest I've gotten:

推荐答案

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

I need similar for my app and have ended up going the long way round. It's a real shame there isn't an easier way to simply switch to a white text version of UIDatePicker.

当 setTextColor: 消息发送到标签时,下面的代码使用 UILabel 上的类别来强制标签的文本颜色为白色.为了不对应用程序中的每个标签执行此操作,我将其过滤为仅在它是 UIDatePicker 类的子视图时应用.最后,一些标签在添加到它们的超级视图之前设置了它们的颜色.为了捕捉这些,代码会覆盖 willMoveToSuperview: 方法.

The code below uses a category on UILabel to force the label's text colour to be white when the setTextColor: message is sent to the label. In order to not do this for every label in the app I've filtered it to only apply if it's a subview of a UIDatePicker class. Finally, some of the labels have their colours set before they are added to their superviews. To catch these the code overrides the willMoveToSuperview: method.

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

It would likely be better to split the below into more than one category but I've added it all here for ease of posting.

#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 lable 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

这篇关于为 iOS7 自定义 UIDatePicker 的文本颜色(就像邮箱一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆