如何快速制作UIPickerView Multiline?

Jur*_*hip 8 uipickerview ios 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
}
Run Code Online (Sandbox Code Playgroud)

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

Har*_*kDG 17

您可以使用自定义视图尝试这样的操作

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;
}
Run Code Online (Sandbox Code Playgroud)

如果您的内容可能超过两行,您还可以设置行高

func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 80
}
Run Code Online (Sandbox Code Playgroud)


Lor*_*ore 7

斯威夫特 4

  • 请记住 aUILabel 的子类UIView
  • 我们正在为视图创建视图,为视图UIPickerView提供文本的其他委托方法现在无关紧要。
  • 如果需要更多空间,请height:在创建期间更改参数UILabel并调整return 80.0行高。
  • 不要忘记设置其他属性,如文本对齐。

首先添加委托方法来指示行高。调整为最适合您的显示目的的值。

func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 80.0
}
Run Code Online (Sandbox Code Playgroud)

接下来添加委托方法来提供视图。

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
}
Run Code Online (Sandbox Code Playgroud)

带有属性文本的示例

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
}
Run Code Online (Sandbox Code Playgroud)