3

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];

ありがとう。

4

1 に答える 1

1

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];
}
于 2018-05-22T20:37:48.973 に答える