ios - iOS 14 PickerView 截断文本
问题描述
我有一个选择器视图,它在 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
}
解决方案
只需在标签上添加 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
}
}
}
推荐阅读
- mysql - mssql 和 mysql 区分大小写
- android - 如何访问 addTextChangedListener 中的视图?
- cassandra - 如何使用pithos将视频文件存储到cassandraDB并将数据复制到另一台机器的另一个节点
- python - Python/Sqlite3:发生异常:sqlite3.OperationalError
- r - R 加速 sapply
- java - 如何将值传递给 JavaBean 中的 List?
- php - Lumen - 如何创建一个可以由 app('request') 从不同文件访问的请求对象?
- bash - 逐列解析CSV文件时比较数值的问题
- r - 使用 read_lines 阅读 pdf 的多页
- excel - 如何允许对受保护的单元格进行格式化和调整大小 - VBA