我目前正在开发一个程序,可以从我的Core Data设置中动态填充选择器视图。我有一切都在数据方面工作,但我现在遇到的问题是在我的标签上格式化。
选择器在操作表中显示自己的工具栏,工具栏右侧有一个按钮。它的初始状态是可见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。我得到了非常奇怪的结果,第二个组件中的一些标签工作得很好。但其他人根本没有包装,有些只是显示为空白。
即:布鲁塞尔豆芽包好(右组分)。但牛奶和奶油不显示(只有牛奶可见)蔬菜根本没有出现。我的代码在哪里出错?
答案 0 :(得分:2)
我设法让它表现得很好。我通过删除
更改了最后一部分
textSize = [title sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:limitSize lineBreakMode:UILineBreakModeWordWrap];
我手动设置了框架
labelRect = CGRectMake(0,0,100.0,36.0);
不知道为什么这个有效,而动态大小没有。
答案 1 :(得分:1)
这是swift的版本。
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
var label : UILabel
if view == nil {
label = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: UIFont.systemFontOfSize(UIFont.systemFontSize()).lineHeight * 2 * UIScreen.mainScreen().scale))
label.textAlignment = NSTextAlignment.Center
label.numberOfLines = 2
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.autoresizingMask = UIViewAutoresizing.FlexibleWidth
} else {
label = view as UILabel
}
label.text = line1 + "\n" + line2
return label;
}
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return UIFont.systemFontOfSize(UIFont.systemFontSize()).lineHeight * 2 * UIScreen.mainScreen().scale
}
答案 2 :(得分:0)
我更正了您的代码,以便让标签宽度适合选择器宽度。 我使用了koushi方法。它现在运作得很好。 请参阅此处附录的代码。
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//assegna i nomi alle caselle del picker e assegna anche la dimensione del font e del rettangolo che contiene le scritte
UILabel *pickerLabel = (UILabel *)view;
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
}
}
CGFloat labelWidth=pickerView.frame.size.width-50;
labelRect = CGRectMake( 0,0,labelWidth,45.0);
if (pickerLabel == nil)
{
pickerLabel = [[UILabel alloc] initWithFrame:labelRect];
[pickerLabel setFont:[UIFont systemFontOfSize:16]];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setNumberOfLines:2];
}
[pickerLabel setText:title];
return pickerLabel;
}
关于您的原始代码:我认为拥有不同矩形元素的选择器不利于外观(用户体验)问题。我怀疑不同的标签rect大小正在解决代码中的问题。如果您要显示的文本可能比定义的rect更大,我认为您应该使用不同的应用程序结构方法。
注意:在我的应用程序中,我使用picker标签属性来识别选择了哪个选择器,因为我的视图中有多个选择器。出于这个原因,我使用以下代码替换了代码中的“switch”语句:
if (pickerView.tag==1)
title= globalSubcategoryArray[row];
else
title= globalAreasName[row];
显然我也补充说:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView.tag==1)
return globalSubcategoryArray.count;
else
return globalAreasName.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (pickerView.tag==1){
self.subcategoryTextview.text = globalSubcategoryArray[row];
globalSelectedSubcategoryIndex=row;
[self.subcategoryTextview resignFirstResponder];
}else{
self.areaTextview.text= globalAreasName[row];
globalSelectedAreaIndex=row;
[self.areaTextview resignFirstResponder];
}
}