0

私は現在、ファイルを読み込んで見出しをリストに入れるアプリを作成しています。各見出しをクリックすると、その見出しに関連付けられたテキスト (通常はいくつかの段落) を含む CATextLayer を持つコントローラーに切り替わります。最初は UITextView を使用していましたが、より多くの書式設定オプション (つまり、正当化された配置) が必要であることがわかり、CATextLayer に切り替えました。

CATextLayer は本質的にスクロール可能であるという他の質問をここで読みましたが、その動作をトリガーする方法が見つかりません。を設定しようとするcontentsRectと、テキストがビューから完全に消えます。

これは、スクロールとは別に動作する私のセットアップ コードです。

    CATextLayer *textLayer = [[CATextLayer alloc] init];
    textLayer.alignmentMode = kCAAlignmentJustified;
    textLayer.string = [_data objectForKey:@"Text"];
    textLayer.wrapped = YES;
    textLayer.frame = CGRectMake(27, 75, 267, 320);
    textLayer.font = [UIFont fontWithName:@"Helvetica" size:17.0f];
    textLayer.foregroundColor = [UIColor blackColor].CGColor;
    textLayer.backgroundColor = [UIColor whiteColor].CGColor;
    [textLayer setCornerRadius:8.0f];
    [textLayer setMasksToBounds:YES]; 
    [textLayer setBorderWidth:1.0f];
    [textLayer setBorderColor:[UIColor grayColor].CGColor];
    [self.view.layer addSublayer:textLayer];
    [textLayer release];

CATextLayer のドキュメントと他の多くの SO の質問に目を通しましたが、探していたものが見つかりませんでした。

編集:contentsRectこのクラスをスクロールすることは可能ですが、のオフセットを使用してプログラムで行う必要があります。CAScrollLayer一部のヘルパー関数 (scrollToPointおよびscrollToRect) と、少し整頓された一連の計算 用にサブレイヤー化できます。CAScrollLayerまた、プログラマーがタッチ イベントを処理する必要があるため、簡単にするために、この方法は追求しません。

でこのルートに行きたい人のためにCAScrollLayer、セットアップコードを次のように変更しました。

    CAScrollLayer  *scrollLayer = [[CAScrollLayer alloc] init];
    scrollLayer.frame = CGRectMake(27, 75, 267, 320);
    scrollLayer.backgroundColor = [UIColor whiteColor].CGColor;
    scrollLayer.scrollMode = kCAScrollVertically;    
    [scrollLayer setCornerRadius:8.0f];
    [scrollLayer setMasksToBounds:YES]; 
    [scrollLayer setBorderWidth:1.0f];
    [scrollLayer setBorderColor:[UIColor grayColor].CGColor];
    scrollLayer.contentsRect = CGRectMake(0, 0, 250, 350);
    scrollLayer.contentsGravity = kCAGravityCenter;

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
    { 
        scrollLayer.contentsScale = [[UIScreen mainScreen] scale];
    }

    CATextLayer *textLayer = [[CATextLayer alloc] init];
    textLayer.alignmentMode = kCAAlignmentJustified;
    textLayer.string = [_notice objectForKey:@"Text"];
    textLayer.wrapped = YES;
    textLayer.frame = CGRectMake(5, 5, 250, 500);
    textLayer.font = [UIFont fontWithName:@"Helvetica" size:17.0f];
    textLayer.foregroundColor = [UIColor blackColor].CGColor;
    textLayer.backgroundColor = [UIColor whiteColor].CGColor;

    [scrollLayer addSublayer:textLayer];

    [self.view.layer addSublayer:scrollLayer];
    [scrollLayer release];
    [textLayer release];
4

1 に答える 1

0

contentRect をどのように使用していますか? これは単位矩形座標です。つまり、各引数は 0 から 1 の間 (両端を含む) です。

したがって、ポイント 30, 30 に 50 x 50 のサブ長方形を表示する場合は、次のようにします。

textLayer.contentsRect = CGRectMake(30.0f / CONTENT_WIDTH, 30.0f / CONTENT_HEIGHT, 267.0f / CONTENT_WIDTH, 320.0f / CONTENT_HEIGHT);

CONTENT_WIDTH と CONTENT_HEIGHT はコンテンツのサイズです。

于 2011-12-20T20:18:47.843 に答える