首页 > 解决方案 > iOS 14 PickerView 截断文本

问题描述

我有一个选择器视图,它在 iOS 14 上无法正确显示文本。有谁知道如何解决这个问题?似乎有一个覆盖文本的子视图?

U被完全切断

是因为我使用的是自定义标签吗?

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let pickerLabel = UILabel()
        let titleData = pickerDataSource[row]
        
        pickerView.subviews[1].backgroundColor = .clear
        pickerView.subviews[0].backgroundColor = .clear
        
        let myTitle = NSAttributedString(string: titleData, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18),NSAttributedString.Key.foregroundColor:UIColor.black])
        pickerLabel.attributedText = myTitle
        return pickerLabel
    }

标签: iosswiftuipickerview

解决方案


只需在标签上添加 margin-left 就可以了。

修复后

原始代码:

extension UILabel {
    func setMargins(margin: CGFloat = 10, _ leftMarginOnly: Bool = true) {
            if let textString = self.text {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.firstLineHeadIndent = margin
                paragraphStyle.headIndent = margin
                if !leftMarginOnly {
                    paragraphStyle.tailIndent = -margin
                }
                let attributedString = NSMutableAttributedString(string: textString)
                attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
                attributedText = attributedString
            }
        }
}

推荐阅读