【问题标题】:Change font size of UITextField or UITextView with UIPickerView [closed]使用 UIPickerView 更改 UITextField 或 UITextView 的字体大小 [关闭]
【发布时间】:2012-03-24 01:46:29
【问题描述】:

我想使用 UIPickerView

更改 UITextFieldUITextView 中的字体大小

【问题讨论】:

    标签: iphone ios uitextfield uitextview uipickerview


    【解决方案1】:

    我建议创建一个 NSArray 来包含选择器中的字体大小。如果你试图给它原语,NSArray 会变得很挑剔,所以在将字体大小添加到你的数组时使用 [NSNumber numberWithInt:(int)]。一旦你有一个数组,填充选择器,然后设置选择器的 didSelectRow: 方法。

    在我的示例中,假设“data”是我的数字数组,“pickerView”是 UIPickerView 的 IBOutlet,“label”是 UILabel 的另一个出口。所有这些代码都将在一个视图控制器中:

       - (void)viewDidLoad
       {
            // pickerView is going to get its data from this class
            pickerView.delegate = self;
            pickerView.dataSource = self;
    
            [super viewDidLoad];
       }
    
        #pragma mark UIPickerViewDelegate methods
        - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
            //Log it out for debugging
            NSLog(@"data for row #%d = %@",row,[data objectAtIndex:row]);
    
            // return data for current row
            return [data objectAtIndex:row];
        }
    
    
        - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
        {
            label.font = [UIFont fontWithName:(NSString *)fontName size:[data objectAtIndex:row]];
        }
    

    我不确定您是否打算存储此值、更改标签或其他什么...但这应该涵盖您需要使用的概念。

    编辑:将格式设置和 viewDidLoad 添加到原始帖子中。

    【讨论】:

    • 嗨,代码中的“fontName”是什么 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { label.font = [UIFont fontWithName:( NSString *)fontName size:[data objectAtIndex:row]]; }
    • 字体名称实际上是我自己的项目中留下的一些代码,我从中复制。您可以查看文档here。出于原始问题的目的,如果您可以使用默认字体,则不需要使用它。如果是这种情况,请继续使用fontWithSize:
    【解决方案2】:

    使用 UIPickerView 的委托,重新设置 UITextField 的字体..

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2011-09-24
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 2012-07-20
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多