a をサブクラス化しようとしていUITextView
ますlayoutManager
しかし、クラッシュが発生し続けます。私が見てきた例はObjective Cにあるので、layoutManagerをどのように変更するべきか少し混乱しています
これが私のコードです...
class TextWrapLayoutManager: NSLayoutManager {
override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
self.enumerateLineFragments(forGlyphRange: NSMakeRange(0, self.numberOfGlyphs)) { (rect, usedRect, textContainer, glyphRange, Bool) in
let lineBoundingRect = self.boundingRect(forGlyphRange: glyphRange, in: textContainer)
let adjustedLineRect = lineBoundingRect.offsetBy(dx: origin.x + 5, dy: origin.y + 2)
let fillColorPath: UIBezierPath = UIBezierPath(roundedRect: adjustedLineRect, cornerRadius: 10)
UIColor.red.setFill()
fillColorPath.fill()
}
}
}
let textView = UITextView(frame: CGRect(x: 0, y: canvasView.center.y - 50, width: UIScreen.main.bounds.width, height: 130))
textView.textContainer.layoutManager = TextWrapLayoutManager()
textView.keyboardAppearance = .dark
textView.textAlignment = .center
textView.font = UIFont.attrctFont(ofSize: 32, weight: .bold)
textView.textColor = textColor
textView.layer.cornerRadius = 10
textView.layer.masksToBounds = true
textView.textContainerInset = UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5)
textView.tintColor = .white
textView.isScrollEnabled = false
textView.delegate = self
self.canvasView.addSubview(textView)
addGestures(view: textView)
textView.becomeFirstResponder()
使用中...
textView.textContainer.layoutManager = TextWrapLayoutManager()
コンパイルはしますが、次の行でクラッシュします。
UITextViewのLayoutManagerをサブクラス化する正しい方法は何ですか?
ありがとう