首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 14 PickerView截断文本

iOS 14 PickerView截断文本
EN

Stack Overflow用户
提问于 2020-09-18 11:23:08
回答 4查看 1.6K关注 0票数 5

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

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

代码语言:javascript
复制
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
    }
EN

回答 4

Stack Overflow用户

发布于 2020-09-22 10:23:05

只需在标签上加上空白处--可以做到这一点。

After the fix

原始代码:

代码语言:javascript
复制
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
            }
        }
}
票数 5
EN

Stack Overflow用户

发布于 2020-09-24 16:24:58

您可以按照Nguyen的建议在ViewForRow方法中设置attributedString,并像这样编辑您的代码

代码语言:javascript
复制
 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
}
票数 1
EN

Stack Overflow用户

发布于 2020-09-28 04:46:49

我使用一种更简单的标签生成方法;现在我的变通方法是使用中心对齐,如下面的示例所示。

代码语言:javascript
复制
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
}

我想知道这是否是字体相关的缺陷,但由于您使用的是系统字体,我得出的结论是它可能不是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63948881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档