我有一个选取器视图,在iOS 14下不能正确显示文本。有人知道如何解决这个问题吗?似乎有一个覆盖文本的子视图?

是因为我使用了自定义标签吗?
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
}发布于 2020-09-22 10:23:05
只需在标签上加上空白处--可以做到这一点。
原始代码:
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
}
}
}发布于 2020-09-24 16:24:58
您可以按照Nguyen的建议在ViewForRow方法中设置attributedString,并像这样编辑您的代码
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])
let margin : CGFloat = 12.0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = margin
paragraphStyle.headIndent = margin
myTitle.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: myTitle.length))
pickerLabel.attributedText = myTitle
return pickerLabel
}发布于 2020-09-28 04:46:49
我使用一种更简单的标签生成方法;现在我的变通方法是使用中心对齐,如下面的示例所示。
func pickerView(_ pickerView: UIPickerView,
viewForRow row: Int,
forComponent component: Int,
reusing view: UIView?) -> UIView
{
let pickerLabel = UILabel()
pickerLabel.text = "Test string"
pickerLabel.font = UIFont(name: "Ropa Sans", size: 18)
pickerLabel.textColor = UIColor.white
pickerLabel.textAlignment = NSTextAlignment.center
}我想知道这是否是字体相关的缺陷,但由于您使用的是系统字体,我得出的结论是它可能不是。
https://stackoverflow.com/questions/63948881
复制相似问题