UIPickerView 左边距

UIPickerView Margin Left

对于带有标题的 UIPickerView,我正在寻找一种为标题提供左边距的方法。目前,前两个词似乎被裁掉了。

我正在使用这个 UIPickerView 方法来分配标题。

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let pickerLabel = UILabel()
    let title = NSAttributedString(string: castProductDetails[row].price, attributes:[.font: FontFamily.Roboto.medium.font(size: 16.0), .foregroundColor: Asset.Colors.primaryText.color] )
    pickerLabel.attributedText = title
    return pickerLabel
}

Adding screenshot for reference.

这对我有用。

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    
    let margin : CGFloat = 20.0
    
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.firstLineHeadIndent = margin
    paragraphStyle.headIndent = margin
    
    let title = NSMutableAttributedString(string: yourArrayHere[row])
    title.addAttribute(.paragraphStyle,
                       value: paragraphStyle,
                       range: NSRange(location: 0, length: title.length))
    
    let pickerLabel = UILabel()
    pickerLabel.attributedText = title
    return pickerLabel
}