通过滚动 UIPickerView 改变字体

changing font by scrolling UIPickerView

是否可以在滚动时更改文本的字体 down/up 具有所有正确系统字体名称的 UIPickerView?以便文本将其字体更改为当前在 pickerView 中选择的字体。

您可以获得所有系统字体并将其用作您的数据源

for(NSString *familyName in [UIFont familyNames]) {
     for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
     NSLog(@"%@", fontName);
   }
}

实施您的委托,您可以为每一行创建自定义视图

- (UIView *)pickerView:(UIPickerView *)pickerView
        viewForRow:(NSInteger)row
      forComponent:(NSInteger)component
       reusingView:(UIView *)view {

  UILabel *pickerLabel = (UILabel *)view;

  if (pickerLabel == nil) {
    CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
    pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    [pickerLabel setTextAlignment:UITextAlignmentLeft];
    [pickerLabel setBackgroundColor:[UIColor clearColor]];
    [pickerLabel setFont:/*font from datasource*/];
  }

  [pickerLabel setText:[pickerDataArray objectAtIndex:row]];

  return pickerLabel;

}

您可以设置一个方法,因为 UIPickerView 在滚动时更改选择 up/down,在 viewdidload 中,或者 UIPickerview 出现的方法,例如

[uorPickerView addTarget:self action:@selector(ValueChanged:) forControlEvents: UIControlEventValueChanged];

您可以在 ValueChanged 方法中设置从 UIPickerview 中选择的字体,如

-(void)ValueChanged :(UIPickerView *) sender {
      //Lets say you have UILable or UITextfield or UITextview as textView, so u can set your text like, 
      // fontArray = Array you have provided for UIPickerView datasource
      [textView setFont:[UIFont fontWithName:[fontArray objectAtIndex:[sender selectedRowInComponent:0]] size:16]]
      // make sure that fontArray has proper value of font family.
} 

希望对您有所帮助,

HTH,享受编码吧!!