我目前正在從我的核心數據設置動態填充選擇器視圖的程序。我有一切工作在數據方面,但我現在遇到的問題是在我的標籤上格式化。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