我目前正在从我的核心数据设置动态填充选择器视图的程序。我有一切工作在数据方面,但我现在遇到的问题是在我的标签上格式化。UIPickerView与多行UILabel
选取器在工具栏的右侧出现一个按钮,在其中显示自己的工具栏。它的初始状态是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。我得到了非常奇怪的结果,第二个组件中的一些标签完美地工作。但其他人根本没有包装,有些只是表现为空白。
ie:抱子甘蓝包好(右分量)。但牛奶和奶油不显示(只有牛奶是可见的)蔬菜根本没有出现。我的代码在哪里出错?
谢谢,这真的帮助了我。 – 2011-05-19 02:44:38