gpt4 book ai didi

ios - 更改不同列的 UIPickerView 字体大小

转载 作者:行者123 更新时间:2023-11-28 18:10:02 25 4
gpt4 key购买 nike

我正在构建一个 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];

}

其中 Number、Season 和 Course 是存储字符串的 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”吗?

更新:感谢 Scotts 的提示(见下文),我的方法有效,发布在这里:

- (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;
}

最佳答案

要自定义选择器的 View ,您必须使用 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;
}

或者不创建标签 subview ,您可以使用 UIPickerViewDelegatepickerView: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];
}

关于ios - 更改不同列的 UIPickerView 字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28077130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com