簡體   English   中英

在NSTextContainer中獲取文本

[英]get text in NSTextContainer

我有一個帶有NSString內容的NSOrderedSet段落。 循環遍歷所有,創建一個大字符串並給出NSTextStorage。 但隨之而來的是所有段落都丟失了。

下面的代碼允許我計算頁面並在頁面中顯示內容。 但我想將段落中的內容分開,並知道用戶何時點擊特定段落。

如何使用NSTextStorage將內容顯示為不是一個單獨的大字符串,而是顯示為較小文本塊的集合? 注意:我正在使用UIPageViewController來顯示內容,因此我還需要知道每頁的段落數。

NSString *content = @"";
for (TWParagraph *paragraph in self.bookParagraphs)
{
    TWTextBlock *textBlock = paragraph.hasTextBlock.firstObject;
    content = [content stringByAppendingString:textBlock.textContent];
}

NSTextStorage *localTextStorage = [[NSTextStorage alloc] initWithString:content];
NSLayoutManager *localLayoutManager = [NSLayoutManager new];
NSTextContainer *localTextContainer = [self createTextContainer];
localLayoutManager.delegate = self;
[localTextStorage addLayoutManager:localLayoutManager];
[localLayoutManager addTextContainer:localTextContainer];

[localLayoutManager ensureLayoutForTextContainer:localTextContainer];


self.numberOfPages = [[localLayoutManager textContainers] count];

謝謝。

我找不到使用NSTextContainer的解決方案。 最后,我通過將文本拆分為UITableView中的單元格來解決它。

每個單元格的高度是根據它應包含的文本量計算的。

- (CGRect) calculateRectSize: (TWTextBlock *) textBlock forAttributes:(NSDictionary *) customAttributes {
    CGSize constraintSize = CGSizeMake(self.tableViewRect.size.width - 10.0f, 400);
    return [[textBlock textContent] boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin attributes:customAttributes context:nil];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM