通过滚动UIPickerView来更改字体

时间:2015-04-19 19:22:40

标签: ios xcode fonts uipickerview

是否可以在向下/向上滚动UIPickerView时更改文本的字体,其中UIPickerView具有系统字体的所有正确名称。这样文本就会将其字体更改为pickerView中当前选择的字体。 ??

2 个答案:

答案 0 :(得分:0)

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

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;

}

答案 1 :(得分:0)

你可以设置一个方法,让UIPickerView在向上/向下滚动时更改选择,在viewdidload中或UIPickerview出现的方法,如

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

您可以在ValueChanged Method中设置从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,享受编码!!