创建一系列NSTextContainer时,如何根据文本内容指定容器分隔符? [英] When creating a series of NSTextContainers, how do I specify container breaks based on the text content?

查看:417
本文介绍了创建一系列NSTextContainer时,如何根据文本内容指定容器分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一系列NSTextContainer来保存HTML资源中的文本.我能够将HTML添加到属性字符串中,将其分配给NSTextStorage和NSLayoutManager,并创建一系列NSTextContainers来容纳所有文本.

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.

我的问题是,我想在文本中添加分页符",即停止填充此文本容器并开始另一个...在文档中,我找到了一个名为NSControlCharacterContainerBreakAction的东西;但是我不清楚如何实现它,甚至还不是最好的方法.

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.

下面的代码段是我当前如何通过文本容器(在Swift中)进行构建.

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

推荐答案

您只需在字符串中放入分页符" ASCII控制字符,布局管理器便会处理它.

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

let pageBreakString = String(UnicodeScalar(12))

参考: ASCII控制字符

这篇关于创建一系列NSTextContainer时,如何根据文本内容指定容器分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆