如何在iOS Objective C中更改日期选择器视图和选择器视图的字体大小

时间:2016-06-07 11:44:56

标签: ios objective-c datepicker uipickerview

我想更改日期选择器和选择器视图的字体大小。这可能吗 ? 如果是吗?然后是怎么做的。

3 个答案:

答案 0 :(得分: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;}

对于Swift

请参阅此内容,了解UIDatePicker

的可自定义实现

https://github.com/prolificinteractive/PIDatePicker

答案 1 :(得分:0)

根据apple's Official site的说明,您无法自定义UIDatepicker

因此,开发者无权更改UIDatepicker。

答案 2 :(得分:0)

实际上你可以使用方法调配来更新字体大小。这就是我改变字体的方式

#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;
}