如何在UIPickerView中进行自动换行

时间:2015-01-18 07:08:06

标签: ios objective-c uilabel uipickerview

我有一个UIPickerView,我想把文字换行。

这是我的代码:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (component == 0) {
    UILabel *lView = (UILabel *)view;

    if (!tView) {
        tView = [[UILabel alloc] init];
        [tView setFont:[UIFont systemFontOfSize:15]];
        tView.numberOfLines = [categorie count];
    }
    tView.text = [categorie objectAtIndex:row];
    return tView;
    }
    else {
        UILabel *lView = (UILabel *)view;

        if (!sView) {
            sView = [[UILabel alloc] init];
            [sView setFont:[UIFont systemFontOfSize:15]];
            sView.numberOfLines = [subCat count];
        }
        sView.text = [subCat objectAtIndex:row];
        return sView;
    }
}

我尝试设置sView.numberOfLines = 0; (你通常会将UILabel设置为自动换行,或者有超过1行),但这并不起作用。

1 个答案:

答案 0 :(得分:0)

让我解释一下代码中发生了什么,例如让我们成像,subCat数组包含两个字符串对象:“one”,“two”

// You try to reuse label for picker
UILabel *sView = (UILabel *)view;
if (!sView) {
    // if this view doesn't exists, you create new label
    sView = [[UILabel alloc] init];
    [sView setFont:[UIFont systemFontOfSize:15]];
    // And once set numberOfLines to value 2 in our case
    sView.numberOfLines = [subCat count];
}
// And here you set text to "one" or "two" value from array
// I don't understand where you set multiline text value?
sView.text = [subCat objectAtIndex:row];
return sView;

你需要这样的东西:

sView.text = [subCat componentsJoinedByString:@"\n"];