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

问题描述

限时删除!!

我遇到了最令人沮丧的困境。我上下研究过,可以清楚地看到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.

我的第一个想法是他们使用的是模仿UIDatePicker的自定义UIPickerView,但我认为情况并非如此。

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

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

1403页,肝出来的..

09-07 01:02