我有一个包含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];谢谢。
发布于 2018-05-23 04:37:49
我找不到使用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];
}https://stackoverflow.com/questions/26183669
复制相似问题