3

すべての例は、テキストの表示/編集に関連するテキストと属性を保持するための「バッキング ストア」として NSMutableAttributedString を示しています。std::string やデータベースのコンテンツなどの代替を使用するにはどうすればよいですか。テストとして、サブクラスを作成し、必要なメソッドをオーバーライドしたときにデフォルト値を返すようにハードコーディングしました。ただし、iPhone 5 デバイスで実行すると、ホーム ボタンを押すまで黒い画面が表示されます。システムは、attributesAtIndex:location:effectiveRange: range: を継続的に呼び出し、CPU 使用率レベルが 100% まで上昇し、アプリは何もしません。string: メソッドを 1 回呼び出しますが、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 はクラス クラスターであると理解しています。これは、抽象クラス ファクトリであることを意味しているようです。別の「バッキング ストア」を使用できない理由がよくわかりません。私の計画は、std::string を使用し (データの取得元の理由により)、一定の属性スタイルを返すことでした (上記のコードのように)。Mac OS X ドキュメントを含め、NSTextStorage、NSLayoutManager、NSTextContainer、および NSTextView についてできる限りのことを読んできました (iOS でこれを行っていますが)。ありがとう!

4

1 に答える 1

2

NSRangePointerは基本的に へのポインタNSRangeです。null でない場合は、メソッドを終了する前に設定する必要があります。このような:

- (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] } ;
}
于 2014-05-03T15:17:01.977 に答える