I have a UIPickerView how can I make it multiline? I have some long text.
Here is my code.
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
}
Here is my viewForRow methode
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;
}
You can try like this with the custom view
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;
}
If you have content that can take more than two lines you can also set the row height
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80
}
UILabel
is a subclass of UIView
.UIPickerView
, other delegate methods that supply text to the views are now irrelevant.height:
parameter during the creation of the UILabel
and adjust the return 80.0
for row height, if even more space is required.First add the delegate method to indicate row height. Adjust to a value that looks best for your display purposes.
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80.0
}
Next add the delegate method to supply the view.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With