更改不同列的UIPickerView字体大小 [英] Change UIPickerView font size for different column
问题描述
我正在构建一个UIPickerView,该代码可以很好地与以下代码配合使用.
I am constructing a UIPickerView, which works fine with my following code.
//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];
}
其中Number,Season和Course是存储字符串的NSArray.在这里,我想将字体大小当然更改为较小的字体.我在这里学到了一些例子.
Where Number, Season and Course are the NSArray storing the string. Here I want to change the font size of course into smaller one. I learned some example here.
- (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"吗?
Is there any quick way for me to combine the above code into my available code to change the font size please? Say, I guess need to pass "Retval" somewhere?
更新:感谢Scotts的提示(见下文),我的方法有效,发布在这里:
UPDATE: Thanks for Scotts's hint(see below), I have my method works,posted here:
- (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;
}
推荐答案
要自定义选择器的视图,必须使用 pickerView:viewForRow:
.为此,请完全删除 pickerView:titleForRow:
,并在 pickerView:viewForRow:
中,使用条件您设置 retval
的文本最初在 pickerView:titleForRow:
中使用.
To customize your picker's views, you have to use pickerView:viewForRow:
. In order to do this, remove pickerView:titleForRow:
entirely, and within pickerView:viewForRow:
, set retval
's text using the conditional you originally used within 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;
}
或者不用创建标签子视图,也可以使用 UIPickerViewDelegate
的 pickerView:attributedTitleForRow:forComponent:
方法直接编辑标题的字体,例如:>
Or instead of creating a label subview, you can directly edit the title's font by using the UIPickerViewDelegate
's pickerView:attributedTitleForRow:forComponent:
method, ex:
- (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];
}
这篇关于更改不同列的UIPickerView字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!