Tag: 文本包

在NSLayoutManager中使用boundingRectForGlyphRange计算字边界时,如何消除前导空格

我在iOS上将多行string分解为单词边界。 我的解决scheme围绕NSLayoutManager的boundingRectForGlyphRange方法。 它几乎可以工作,除了每个单词的矩形是向右几个像素。 换句话说,NSLayoutManager似乎是在每一行添加一个前导空格/缩进,我找不到任何方法来覆盖此行为。 我试过使用NSLayoutManager.usesFontLeading以及NSParagraphStyle.headIndent,但没有任何结果: NSLayoutManager* layout = [NSLayoutManager new]; layout.usesFontLeading = NO; NSMutableParagraphStyle* paragraphStyle = [NSMutableParagraphStyle new]; paragraphStyle.headIndent = paragraphStyle.firstLineHeadIndent = 0; NSTextStorage* textStorage = [[NSTextStorage alloc] initWithString:self attributes:@{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle}]; layout.textStorage = textStorage; NSTextContainer* textContainer = [[NSTextContainer alloc] initWithSize:size]; [layout addTextContainer:textContainer]; // compute bounding rect for each word range for (NSValue* wordRangeValue in wordRanges) […]

在创build一系列NSTextContainers时,如何根据文本内容指定容器中断?

我创build了一系列NSTextContainers来保存来自HTML资源的文本。 我能够将HTML添加到一个属性string,将其分配给NSTextStorage和NSLayoutManager,并创build一系列NSTextContainers来保存所有文本。 我的问题是,我想在文本中添加“分页符”,即停止填充此文本容器,并开始另一个…在文档中,我find了一些调用NSControlCharacterContainerBreakAction; 但我不清楚如何实施,或者即使是最好的方法。 下面的代码片段是我如何通过文本容器(在Swift中)构build的。 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) […]