我目前正在开发一个从我的核心数据设置中动态填充选择器视图的程序。我的一切都在数据方面工作,但我现在遇到的问题是我的标签上的格式。
选择器在操作表中显示了它自己的工具栏,工具栏右侧有一个按钮。它的初始状态是可见 2 个刻度盘。按下按钮时,它变为 3 个刻度盘。
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *pickerLabel = (UILabel *)view;
CGSize limitSize = CGSizeMake(100.0f, 45.0f);
CGSize textSize;
CGRect labelRect;
NSString *title = @"";
switch (numberOfComponents){
case 2:
{
...gets strings from fetched data (varying length from 4 to 20+)
title = someString
}
case 3:
{
...same as above but for the second set of data.
title = someString
}
}
textSize = [title sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:limitSize lineBreakMode:UILineBreakModeWordWrap];
labelRect = CGRectMake(0, 0, textSize.width, textSize.height);
NSLog(@"length:%i title:%@",[title length],title);
NSLog(@"h:%f w:%f",textSize.height,textSize.width);
if (pickerLabel == nil)
{
pickerLabel = [[[UILabel alloc] initWithFrame:labelRect] autorelease];
[pickerLabel setFont:[UIFont systemFontOfSize:14]];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setLineBreakMode:UILineBreakModeWordWrap];
[pickerLabel setTextAlignment:UITextAlignmentCenter];
[pickerLabel setNumberOfLines:2];
}
[pickerLabel setText:title];
return pickerLabel;
}
我手动将行高设置为 32.0f。我得到了非常奇怪的结果,因为第二个组件中的一些标签工作正常。但其他人根本没有换行,有些人只是显示为空白。
即:球芽甘蓝包裹好(右组件)。但牛奶和奶油不显示(只有牛奶可见)蔬菜根本不出现。我的代码哪里出错了?