如何更改UIPickerView的文本大小和组件宽度? [英] How do I change the text size and component width of a UIPickerView?

查看:374
本文介绍了如何更改UIPickerView的文本大小和组件宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来创建UIPickerView:

I have the following code to create a UIPickerView:

pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f, 416.0f - height, 320.0f, height)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[pickerView setSoundsEnabled:YES];

我想更改组件宽度并更改每个组件的文本大小。是否可以这样做?

I would like to change the component widths and change the text size in each component. Is it possible to do this?

谢谢!

推荐答案

你可以通过适当的委托方法更改宽度

You can change the width by an appropriate delegate method

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    switch(component) {
        case 0: return 22;
        case 1: return 44;
        case 2: return 88;
        default: return 22;
    }

    //NOT REACHED
    return 22;
}

对于自定义文本大小,您可以使用委托返回自定义视图您想要的任何大小的文本:

As for a custom text size, you can use the delegate to return custom views with whatever sized text you want:

- (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.text = @"Demo";
        retval.font = [UIFont systemFontOfSize:22];
        return retval;
}

当然,您需要修改这些以获得适合您应用的值,但是它应该让你到达你需要去的地方。

Of course you will need modify these to have appropriate values for your app, but it should get you where you need to go.

这篇关于如何更改UIPickerView的文本大小和组件宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆