【问题标题】:how to change font size of date picker view and picker view in iOS Objective C如何在 iOS Objective C 中更改日期选择器视图和选择器视图的字体大小
【发布时间】:2016-06-07 11:44:56
【问题描述】:
我想更改日期选择器和选择器视图的字体大小。这可能吗 ?
如果是?然后是怎么做的。
【问题讨论】:
-
您似乎希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的编码人员编写代码,但他们通常只有在发布者已经尝试自己解决问题时才会提供帮助。展示这项工作的一个好方法是包含您迄今为止编写的代码、示例输入(如果有的话)、预期输出以及您实际获得的输出(控制台输出、回溯等)。您提供的详细信息越多,您可能收到的答案就越多。检查FAQ 和How to Ask。
-
标签:
ios
objective-c
datepicker
uipickerview
【解决方案1】:
UIDatePicker没有任何改变外观的API。
您可以使用 UIPickerView 自己制作一个非常令人信服的副本
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return a;}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return b;}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *lblpkr= [[UILabel alloc] initWithFrame:CGRectMake(p,q,r,s)];
[lblpkr setBackgroundColor:[UIColor clearColor]];
[lblpkr setTextColor:[UIColor blueColor]];
[lblpkr setFont:[UIFont boldSystemFontOfSize:x]];
[lblpkr setText:[NSString stringWithFormat:@"%d",row]];
return lblpkr;}
对于斯威夫特
请参阅此处了解UIDatePicker 的可定制实现
https://github.com/prolificinteractive/PIDatePicker
【解决方案3】:
其实你可以使用 swizzling 方法来更新字体大小。这就是我改变字体的方式
#import "UILabel+Extra.h"
@implementation UILabel (Extra)
-(void)layoutSubviews{
[super layoutSubviews];
NSArray *arrayFont = [self.font.fontName componentsSeparatedByString:@"-"];
NSArray *arrayRobotoStyleName = [UIFont fontNamesForFamilyName:@"Roboto"];
__block NSString *style;
if(arrayFont.count >= 2){
[arrayRobotoStyleName enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger index, BOOL * _Nonnull stop){
NSString *styleName = [arrayFont lastObject];
if ([obj containsString:styleName]){
style = styleName;
*stop = TRUE;
}
else if (index == ([arrayRobotoStyleName count] - 1)){
style = @"Regular";
*stop = TRUE;
}
}];
}
else
style = @"Regular";
if (!style) {
style = @"Regular";
}
self.font = [UIFont fontWithName:[NSString stringWithFormat:@"Roboto-%@",style] size:self.font.pointSize];
}
-(void)setAppearanceFont:(UIFont *)font{
if (font)
[self setFont:font];
}
-(UIFont *)appearanceFont{
return self.font;
}