How can I make my UIPickerView Multiline fast?

I have a UIPickerView, how can I make it multi-line? I have a 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 method

 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; } 
+5
source share
2 answers

You can try, like this, with a 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 line height

 func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 80 } 
+10
source

You will have to change rowHeightForComponent (UIPickerView)

Sort of:

  func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 70.0 } 

And you can see it in multiline, everything else that you did well.

0
source

Source: https://habr.com/ru/post/1247370/


All Articles