更改不同列的UIPickerView字体大小
我正在构build一个UIPickerView,这与我的下面的代码工作正常。
//Title for Row - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component==kNumComponent) return Number[row]; else if(component==kSeaComponent) return Season[row]; else return Course[row]; }
数字,季节和课程是存储string的NSArray。 在这里,我想改变当然的字体大小为较小的一个。 我在这里学到了一些例子。
- (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]; retval.text = [pickerViewArray objectAtIndex:row]; return retval; }
有没有什么快速的方法可以将上面的代码合并到我的可用代码中来更改字体大小? 说,我想需要通过“Retval”的地方?
更新:感谢史考特的提示(见下文),我有我的方法作品,张贴在这里:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *retval = (UILabel*)view; if (!retval) { retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)]; } retval.font = [UIFont systemFontOfSize:22]; if (component==kNumComponent) retval.text = Number[row]; else if(component==kSeaComponent) {retval.font = [UIFont systemFontOfSize:14]; retval.text = Season[row];} else { retval.font = [UIFont systemFontOfSize:14]; retval.text = Course[row];} return retval; }
要自定义你的select器的视图,你必须使用pickerView:viewForRow:
为了做到这一点,删除pickerView:titleForRow:
完全,并在pickerView:viewForRow:
设置retval
的文本使用条件你最初使用pickerView:titleForRow:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *retval = (UILabel*)view; if (!retval) { retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)]; } retval.font = [UIFont systemFontOfSize:22]; if (component==kNumComponent) retval.text = Number[row]; else if(component==kSeaComponent) retval.text = Season[row]; else retval.text = Course[row]; return retval; }
或者不是创build一个标签子视图,你可以直接编辑标题的字体,使用UIPickerViewDelegate
的pickerView:attributedTitleForRow:forComponent:
方法,例如:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString *title; if (component==kNumComponent) title = Number[row]; else if(component==kSeaComponent) title = Season[row]; else title = Course[row]; UIFont *font = [UIFont systemFontOfSize:22]; NSDictionary *attributes = @{NSFontAttributeName:font}; return [[NSAttributedString alloc] initWithString:title attributes:attributes]; }