xcode 在 Pickerview 中更改文本大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2589163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:15:55  来源:igfitidea点击:

Changing text size in Pickerview

iphoneobjective-ccocoa-touchxcode

提问by isarathg

How can we change the size of the text in a picker view?

我们如何在选择器视图中更改文本的大小?

Anyone please help.

任何人请帮忙。

回答by MohammedYakub Moriswala

You can change the font size of UIPickerView using code :

您可以使用代码更改 UIPickerView 的字体大小:

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
            UILabel *retval = (id)view;
            if (!retval) {
                retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
            }

            retval.font = [UIFont systemFontOfSize:22];

       return retval;
    }

回答by Luca

For completeness, yakub_moriss code lacks of one line, which was in the post signaled by indragie. Without a line as

为完整起见,yakub_moriss 代码缺少一行,这是在 indragie 发信号的帖子中。没有线作为

retval.text = [pickerViewArray objectAtIndex:row];

the picker will be empty.

选择器将为空。

回答by Andrew Forget

This is also a great way to change the alignment of the text in the component too, change the textAlignment of the retval label.

这也是更改组件中文本对齐方式的好方法,更改 retval 标签的 textAlignment。

Also, the background for the label should be set to clear:

此外,标签的背景应设置为清除:

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        UILabel *retval = (id)view;
        if (!retval) {
            retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 
                      [pickerView rowSizeForComponent:component].width, 
                      [pickerView rowSizeForComponent:component].height)];
        }
        retval.opaque = NO;
        retval.backgroundColor = [UIColor clearColor];
        retval.font = [UIFont systemFontOfSize:28];
        if (component == 0) {
            retval.textAlignment = UITextAlignmentRight;
            retval.text = [NSString stringWithFormat:@"%d ", row];
        } else {
            retval.textAlignment = UITextAlignmentLeft;
            retval.text = [NSString stringWithFormat:@" .%d", row];
        }
        return retval;
    }

    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
    {
        if (component == 0) { 
            return 100;
        }
        return 88;
    }

回答by Dan Rosenstark

I'm using a category for this. It relies on this other category I have: http://compileyouidontevenknowyou.blogspot.com/2012/06/adding-properties-to-class-you-dont.html.

我为此使用了一个类别。它依赖于我拥有的其他类别:http: //compileyouidontevenknowyou.blogspot.com/2012/06/adding-properties-to-class-you-dont.html

Anyway, this one looks like this:

无论如何,这个看起来像这样:

#import <UIKit/UIKit.h>

@interface UIPickerView (CustomViewForRow)

@property (nonatomic, strong) UIFont *customViewForRowFont;

- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

@end

and the .m

还有他们

#import "UIPickerView+CustomViewForRow.h"
#import "NSObject+AssociatedObjectSupport.h"

@implementation UIPickerView (CustomViewForRow)


static char keyForCustomViewForRowFont;
- (UIFont *)customViewForRowFont {
    UIFont *retVal = [self associatedObject:&keyForCustomViewForRowFont];
    if (!retVal)
        retVal = [UIFont boldSystemFontOfSize:17];
    return retVal;
}

- (void)setCustomViewForRowFont:(UIFont *)customViewForRowFont {
    [self setAssociatedObject:customViewForRowFont forKey:&keyForCustomViewForRowFont];
}

- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label;
    if (!view) {
        view = [[UIView alloc] init];
        CGRect frame = CGRectZero;
        frame.size = [self rowSizeForComponent:component];
        view.frame = frame;
        view.backgroundColor = UIColor.clearColor;

        label = [[UILabel alloc] init];
        label.font = self.customViewForRowFont;
        label.backgroundColor = [UIColor clearColor];
        frame = view.bounds;
        frame.origin.x += 7;
        frame.size.width -= 14;
        label.frame = frame;
        [view addSubview:label];
    }

    label = [view.subviews objectAtIndex:0];
    @try {
        NSString *text = [self.delegate pickerView:self titleForRow:row forComponent:component];
        if (!text)
            text = @"???";
        label.text = text;
    } @catch (NSException *e) {
        label.text = @"???";
    }

    return view;
}

@end

回答by Ale? Oskar Kocur

Try this https://github.com/kafejo/CCPickerit's very easy picker :)

试试这个 https://github.com/kafejo/CCPicker这是非常简单的选择器:)