When creating a series of NSTextContainers, how do I specify container breaks based on the text content?

◇◆丶佛笑我妖孽 提交于 2019-12-22 09:22:36

问题


I'm creating a series of NSTextContainers to hold the text from an HTML resource. I am able to add the HTML to an attributed string, assign that to a NSTextStorage and NSLayoutManager, and create a series of NSTextContainers to hold all the text.

My problem is, I want to add "page breaks" within the text, i.e. stop filling this text container and start another... In the documentation, I've found something call NSControlCharacterContainerBreakAction; but I'm unclear how to implement it or if thats even the best approach.

Code snippet below is how I'm current building by text containers (in Swift).

var myLayoutManager = NSLayoutManager()
var myText:NSAttributedString = {
        let path = NSBundle.mainBundle().URLForResource("localfile", withExtension: "html")
        let opts = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
        return NSMutableAttributedString(fileURL: path, options: nil, documentAttributes: nil, error: nil)!
        }()

myTextStorage = NSTextStorage(attributedString: myText)
myTextStorage.addLayoutManager(myLayoutManager)

//Create all textContainers to hold text
if myLayoutManager.textContainers.count == 0 {
    var range = NSMakeRange(0, 0)
    while(NSMaxRange(range) < myLayoutManager.numberOfGlyphs) {
        var myTextContainer = NSTextContainer(size: CGSizeMake(450, 580))
        myLayoutManager.addTextContainer(myTextContainer)
        range = myLayoutManager.glyphRangeForTextContainer(myTextContainer)
    }
}

回答1:


You can just put "Page Break" ASCII control character in your string, layout manager will handle it.

let pageBreakString = String(UnicodeScalar(12))

Reference: ASCII Control Character



来源:https://stackoverflow.com/questions/27970541/when-creating-a-series-of-nstextcontainers-how-do-i-specify-container-breaks-ba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!