我有一个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行),但这并不起作用。
答案 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"];