Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my UIPickerView Multiline in swift?

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;
}
like image 860
Jurian Amatsahip Avatar asked Apr 18 '16 14:04

Jurian Amatsahip


2 Answers

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
}
like image 93
HardikDG Avatar answered Oct 15 '22 18:10

HardikDG


Swift 4

  • Keep in mind a UILabel is a subclass of UIView.
  • We are creating the views for the UIPickerView, other delegate methods that supply text to the views are now irrelevant.
  • Change the height: parameter during the creation of the UILabel and adjust the return 80.0 for row height, if even more space is required.
  • Don't forget to set additional properties like text alignment.

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
}

Example with Attributed Text

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
}
like image 38
Lore Avatar answered Oct 15 '22 20:10

Lore