Customize text color of UIDatePicker for iOS7 (just like Mailbox does)(为 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 的文本颜色(就像邮箱一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 iOS7 自定义 UIDatePicker 的文本颜色(就像邮箱一样)
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01