iOS 14 PickerView 截断文本 [英] iOS 14 PickerView cutting off text
本文介绍了iOS 14 PickerView 截断文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选择器视图,它不能像 iOS 14 那样正确显示文本.有谁知道如何解决这个问题?似乎有一个覆盖文本的子视图?
I Have a pickerview which does not display text properly as off iOS 14. Does anyone know how to fix this? It seems like there is a subview covering the text?
是不是因为我使用了自定义标签?
Is it because I'm using a custom label?
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
}
推荐答案
只需在标签中添加 margin-left 即可.
Just add margin-left to the label would do the trick.
原始代码:
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
}
}
}
这篇关于iOS 14 PickerView 截断文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文