gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:57:29 24 4
gpt4 key购买 nike

刚刚下载了我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com