CATextLayer 的文字被截断

问题描述

我想在 UIView 中显示 CATextLayer 并更改 textlayer 的文本和字体。但是 textlayer 中的文本切断了右侧的设备。

最奇怪的是,如果我在后台发送应用程序然后再次打开它,问题就消失了!!

我的代码

  import UIKit

class FontDetailViewController: UIViewController {

@IBOutlet weak var textField: UITextView!
@IBOutlet weak var layerTextView: UIView!

var font: Font!
let textLayer = CATextLayer()

override func viewDidLoad() {
    super.viewDidLoad()
    let titleFor = font.fontName.capitalized
    title = titleFor.replacingOccurrences(of: #"\b-"#,with: "",options: .regularExpression,range: nil)
    textLayer.frame = layerTextView.bounds


    setUpView(for: font)
    layerTextView.layer.masksToBounds = true
    layerTextView.layer.addSublayer(textLayer)
    
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    textLayer.frame = layerTextView.bounds
    
}


func setUpView(for font: Font) {
    textField.font =  font.getUIFont()
    setUpTextLayer(font.getCTFont())

    
}

func setUpTextLayer(_ font: CTFont) {
    
    textLayer.frame = layerTextView.bounds
    let string = "Lorem ipsum dolor sit er elit lamet,consectetaur cillium adipisicing pecu,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."
    
    textLayer.string = string
    textLayer.font = font
    textLayer.fontSize = 14
    textLayer.foregroundColor = UIColor.darkGray.cgColor
    textLayer.isWrapped = true
    textLayer.alignmentMode = .center
    textLayer.truncationMode = .middle
    textLayer.contentsScale = UIScreen.main.scale
    
}
}

但是文本在 CTFont 中像这样在右侧被截断

problem

但是如果我在后台发送应用程序并再次打开它,那么问题就这样消失了,

enter image description here

有谁能帮帮我吗? :(

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)