首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取NSTextContainer中的文本

获取NSTextContainer中的文本
EN

Stack Overflow用户
提问于 2014-10-04 01:19:12
回答 1查看 569关注 0票数 3

我有一个包含NSString内容的段落的NSOrderedSet。循环遍历所有内容,创建一个大字符串并为其提供NSTextStorage。但是,所有的段落都丢失了。

下面的代码允许我计算页面数并按页面显示内容。但是我想把段落中的内容分开,并知道用户什么时候点击了特定的段落。

如何使用NSTextStorage将内容不显示为单个大字符串,而显示为较小文本块的集合?注意:我使用UIPageViewController来显示内容,所以我还需要知道每页的段落数。

代码语言:javascript
复制
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];

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-05-23 04:37:49

我找不到使用NSTextContainer的解决方案。最后,我通过将文本拆分成UITableView中的单元格解决了这个问题。

每个单元格的高度是根据它应该包含的文本量计算的。

代码语言:javascript
复制
- (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];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26183669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档