必要な属性付きテキストを含む各ページに UITextView を使用して複数ページの PDF を作成しようとしています。しかし、アプリがアーカイブされ、テストのためにTestFlightを介して配布されたときに問題が発生した場合。
以下は、マルチページを生成するために使用したサンプルコードです。
var textStorage = NSTextStorage()
textStorage = NSTextStorage(attributedString: attString)
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
var pageSize = CGRect(x: 44, y: 108, width: 507, height: 690)
var lastGlyph = 0
while lastGlyph < layoutManager.numberOfGlyphs {
let textContainer = NSTextContainer()
let background = UINib(nibName: "background", bundle: nil).instantiate(withOwner: nil, options: nil)[0]
background.frame = pageRect
textContainer.size = subsequentPageSize.size
layoutManager.addTextContainer(textContainer)
let textView = UITextView(frame: pageSize, textContainer: textContainer)
pageSize.origin.x += pageSize.width
background.addSubview(textView)
context.beginPage()
background.layer.render(in: UIGraphicsGetCurrentContext()!)
lastGlyph = NSMaxRange(layoutManager.glyphRange(for: textContainer))
}
これは、シミュレーターまたは Xcode からビルドされたデバイスで実行すると完全に正常に動作しますが、アプリが配布されるとすぐに、layoutManager.numberOfGlyphs
表示されるレイアウトマネージャーを print() しても、常に 0 が返されます。
<NSLayoutManager: 0x7ff313c9f6d0>
0 containers, text backing has 57 characters
Currently holding 57 glyphs.
Glyph tree contents: 57 characters, 57 glyphs, 1 nodes, 64 node bytes, 64 storage bytes, 128 total bytes, 2.25 bytes per character, 2.25 bytes per glyph
Layout tree contents: 57 characters, 57 glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, 64 node bytes, 0 storage bytes, 64 total bytes, 1.12 bytes per character, 1.12 bytes per glyph, 0.00 laid glyphs per laid line fragment, 0.00 bytes per laid line fragment'.
何かばかげたことを見逃したのでしょうか、それとも私が気付いていないバグがありますか? なぜそれが機能しないのか、一生理解できません!
与えられた助けに感謝します。