2

I want to show a CATextLayer inside of a UIView and change the text and the font of the textlayer. But texts in textlayer cut off the device in the right side.

Most Weird thing is, if I send app in background and then open it again, the problem gone !!

My Code:

  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
    
}
}

But text cut off in the right side like this in CTFont

problem

But if I send app in background and open it again, then the problem disappears like that,

enter image description here

Is there anyone who can help me? :(

Duncan C
  • 128,072
  • 22
  • 173
  • 272
Shahriar Nasim Nafi
  • 1,160
  • 15
  • 19

0 Answers0