【问题标题】:get text in NSTextContainer在 NSTextContainer 中获取文本
【发布时间】:2014-10-03 17:19:12
【问题描述】:

我有一个包含 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];

谢谢。

【问题讨论】:

  • 你找到解决方案了吗?
  • @VladPulichev,我发布了我最终使用的解决方案。希望对您有所帮助。

标签: ios textkit nslayoutmanager nstextstorage nstextcontainer


【解决方案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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多