繁体   English   中英

NSLayoutManager numberOfGlyphs 总是显示 0,Swift

[英]NSLayoutManager numberOfGlyphs always showing 0, Swift

我正在尝试使用所需的属性文本在每个页面上创建一个带有 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总是返回 0,即使我打印()它显示的layoutManager.numberOfGlyphs

    <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'.

我是否错过了一些愚蠢的事情,或者是否存在我不知道的错误? 我一生都无法理解为什么它不起作用!

感谢可以提供的任何帮助。

我最终在 NSTextStorage 子类化注释中找到了一个解决方案,其中指出,

NSTextStorage 类实现更改管理(通过 beginEditing() 和 endEditing() 方法)、属性验证、委托处理和布局管理通知。 它没有实现的一方面是管理实际的属性字符串存储,子类通过覆盖两个 NSAttributedString 原语来管理......

因此,我更改了我的代码并将 textStorage.beginEditing() 和 textStorage.endEditing() 添加到整个序列的开头和结尾,如下所示,一旦项目实现,它现在就可以工作,也可以从 Xcode 直接构建到设备或模拟器。

var textStorage = NSTextStorage()
textStorage.beginEditing()
if attribString != nil {
    textStorage = NSTextStorage(attributedString: attribString)
} else {
    textStorage = NSTextStorage(string: string)
}

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)     
    background.addSubview(textView)     
    context.beginPage()     
    background.layer.render(in: UIGraphicsGetCurrentContext()!)    
    lastGlyph = NSMaxRange(layoutManager.glyphRange(for: textContainer))
}
textStorage.endEditing()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM