如何在 swift 中使我的 UIPickerView 多行显示?
How can I make my UIPickerView Multiline in swift?
我有一个 UIPickerView,如何让它多行显示?我有一些长文本。
这是我的代码。
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var returnResult: String = ""
if pickerView.tag == 1 {
//This picker has long text
let position: UInt = UInt(row)
returnResult = list.objectAtIndex(position).Description.Value;
} else if pickerView.tag == 2 {
returnResult = questionTwoOptions[row]
} else if pickerView.tag == 3 {
returnResult = questionThreeOptions[row]
} else {
returnResult = ""
}
print(returnResult)
return returnResult
}
这是我的 viewForRow 方法
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let label = UILabel(frame: CGRectMake(0, 0, 400, 44));
label.lineBreakMode = .ByWordWrapping;
label.numberOfLines = 0;
//view!.addSubview(label)
return label;
}
您将不得不更改 rowHeightForComponent (UIPickerView)
类似于:
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
{
return 70.0
}
而且您将能够在多行中看到它,其余的一切您都做得很好。
您可以尝试使用自定义视图
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let label = UILabel(frame: CGRectMake(0, 0, 400, 44));
label.lineBreakMode = .ByWordWrapping;
label.numberOfLines = 0;
label.text = arr[row]
label.sizeToFit()
return label;
}
如果您的内容超过两行,您还可以设置行高
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80
}
Swift 4
- 请记住
UILabel
是 UIView
的子类。
- 我们正在为
UIPickerView
创建视图,其他向视图提供文本的委托方法现在无关紧要。
- 在创建
UILabel
期间更改 height:
参数并调整 return 80.0
行高,如果需要更多 space。
- 不要忘记设置文本对齐等其他属性。
首先添加指示行高的委托方法。调整为最适合您的显示目的的值。
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80.0
}
接下来添加委托方法来提供视图。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label: UILabel
if let view = view {
label = view as! UILabel
}
else {
label = UILabel(frame: CGRect(x: 0, y: 0, width: pickerView.frame.width, height: 400))
}
label.text = myTitleStrings[row]
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.sizeToFit()
return label
}
带有属性文本的示例
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label: UILabel
if let view = view {
label = view as! UILabel
}
else {
label = UILabel(frame: CGRect(x: 0, y: 0, width: pickerView.frame.width, height: 400))
}
let title = myTitleStrings[row]
let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue", size: 18.0)!]
label.attributedText = NSAttributedString(string: title, attributes: attributes)
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.sizeToFit()
return label
}
我有一个 UIPickerView,如何让它多行显示?我有一些长文本。
这是我的代码。
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var returnResult: String = ""
if pickerView.tag == 1 {
//This picker has long text
let position: UInt = UInt(row)
returnResult = list.objectAtIndex(position).Description.Value;
} else if pickerView.tag == 2 {
returnResult = questionTwoOptions[row]
} else if pickerView.tag == 3 {
returnResult = questionThreeOptions[row]
} else {
returnResult = ""
}
print(returnResult)
return returnResult
}
这是我的 viewForRow 方法
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let label = UILabel(frame: CGRectMake(0, 0, 400, 44));
label.lineBreakMode = .ByWordWrapping;
label.numberOfLines = 0;
//view!.addSubview(label)
return label;
}
您将不得不更改 rowHeightForComponent (UIPickerView)
类似于:
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
{
return 70.0
}
而且您将能够在多行中看到它,其余的一切您都做得很好。
您可以尝试使用自定义视图
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let label = UILabel(frame: CGRectMake(0, 0, 400, 44));
label.lineBreakMode = .ByWordWrapping;
label.numberOfLines = 0;
label.text = arr[row]
label.sizeToFit()
return label;
}
如果您的内容超过两行,您还可以设置行高
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80
}
Swift 4
- 请记住
UILabel
是UIView
的子类。 - 我们正在为
UIPickerView
创建视图,其他向视图提供文本的委托方法现在无关紧要。 - 在创建
UILabel
期间更改height:
参数并调整return 80.0
行高,如果需要更多 space。 - 不要忘记设置文本对齐等其他属性。
首先添加指示行高的委托方法。调整为最适合您的显示目的的值。
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80.0
}
接下来添加委托方法来提供视图。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label: UILabel
if let view = view {
label = view as! UILabel
}
else {
label = UILabel(frame: CGRect(x: 0, y: 0, width: pickerView.frame.width, height: 400))
}
label.text = myTitleStrings[row]
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.sizeToFit()
return label
}
带有属性文本的示例
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label: UILabel
if let view = view {
label = view as! UILabel
}
else {
label = UILabel(frame: CGRect(x: 0, y: 0, width: pickerView.frame.width, height: 400))
}
let title = myTitleStrings[row]
let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue", size: 18.0)!]
label.attributedText = NSAttributedString(string: title, attributes: attributes)
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.sizeToFit()
return label
}