【问题标题】:Alternate backing store for NSTextStorageNSTextStorage 的备用后备存储
【发布时间】:2014-02-19 00:15:11
【问题描述】:

每个示例都显示一个 NSMutableAttributedString 作为“后备存储”,用于保存与查看/编辑文本相关的文本和属性。我如何使用替代品,例如 std::string 或者数据库中的内容。作为一个测试,我创建了一个子类并对其进行硬编码,以便在我覆盖所需的方法时返回默认值。但是,当我在 iPhone 5 设备上运行它时,它只会显示黑屏,直到我按下 Home 按钮。系统不断调用attributesAtIndex:location:effectiveRange:range:,CPU使用率上升到100%,App从不做任何事情。它确实调用了一次 string: 方法,然后只是继续调用 attributesAtIndex:location:effectiveRange:range

例子:

@implementation MyTextStorage
{
}

- (id)init
{
    self = [super init];

    if (self)
    {
    }

    return self;
}


#pragma mark - Reading Text

- (NSString *)string
{
    return @"Static"; 
}

- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
{
    return  @{NSFontAttributeName:  [UIFont fontWithName:@"Noteworthy-Bold" size:36] } ;
}

#pragma mark - Text Editing

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
    // Empty - Don't allow editing
}

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
{
    // Empty - Don't allow editing
}

- (void)processEditing
{
    [super processEditing];
}




@end

这是它的设置方式:

    // property
    self.textStorage = [MyTextStorage new];

    // Create layout manager, and attach text storage to layout manager.
    NSLayoutManager* layoutManager = [[NSLayoutManager alloc] init];
    [self.textStorage addLayoutManager: layoutManager];

    // Create text container and attach to layout manager
    CGSize size = self.view.bounds.size;
    size.height = size.height/2;

    NSTextContainer* textContainer = [[NSTextContainer alloc] initWithSize: size];
    [layoutManager addTextContainer: textContainer];

    // Create text view, given text container.
    CGRect frame1 = CGRectMake(0, 20, size.width, size.height);
    UITextView *tv1 = [[UITextView alloc] initWithFrame:frame1 textContainer: textContainer];
    [self.view addSubview: tv1];

我理解 NSTextStorage 是一个 Class Cluster,这似乎意味着它是一个抽象类工厂。我真的不明白为什么我不能使用另一个“后备商店”。我的计划是使用 std::string (因为我的数据来自哪里),然后返回一个常量属性样式(如上面的代码)。我一直在阅读有关 NSTextStorage、NSLayoutManager、NSTextContainer 和 NSTextView 的所有内容,包括 mac OS X 文档(即使我在 iOS 上这样做)。谢谢!

【问题讨论】:

    标签: ios textkit nstextstorage


    【解决方案1】:

    NSRangePointer 基本上是一个指向NSRange 的指针。 如果它不为空,则需要在退出方法之前设置它。像这样:

    - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range {
        if(range != NULL){
            range->location = 0;
            range->length = self.string.length;
        }
        return  @{NSFontAttributeName:  [UIFont fontWithName:@"Noteworthy-Bold" size:36] } ;
    }
    

    【讨论】:

    • 抱歉,这需要一段时间来测试,你没看错。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2012-04-09
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多