繁体   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