在Pickerview中更改文本大小

时间:2010-04-07 00:01:27

标签: iphone objective-c cocoa-touch xcode

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

任何人都请帮忙。

5 个答案:

答案 0 :(得分:6)

您可以使用代码更改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;
    }

答案 1 :(得分:4)

为了完整性,yakub_moriss代码缺少一行,这是由indragie发出的帖子。没有一行

retval.text = [pickerViewArray objectAtIndex:row];

选择器将为空。

答案 2 :(得分:3)

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

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

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

答案 3 :(得分:0)

我正在使用一个类别。它依赖于我的另一个类别:http://compileyouidontevenknowyou.blogspot.com/2012/06/adding-properties-to-class-you-dont.html

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

#import <UIKit/UIKit.h>

@interface UIPickerView (CustomViewForRow)

@property (nonatomic, strong) UIFont *customViewForRowFont;

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

@end

和.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

答案 4 :(得分:0)

试试这个 https://github.com/kafejo/CCPicker 这是非常容易的选择:)