微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CATextLayer 的文字被截断

如何解决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 举报,一经查实,本站将立刻删除。