2011-06-27 9 views
1

私は数日間この作業を続けてきました。私が持っている問題は、(テキスト文字列を倍にすることで)UIViewコンテンツを更新すると、スーパービュー(UIScrollView)は更新されたコンテンツをダブルサイズで表示するということです。iOS UIScrollView/UIViewは更新されたコンテンツをダブルサイズで表示します

基本的に、IBで作成されたUIScrollViewを持つUIViewControllerがあります。 UIScrollViewは、UIViewの子プログラムをプログラムで作成します。 UIViewは小さな「Loading ...」テキスト(Core Textを介して)を表示し、URLからロードされている実際のコンテンツに変更されます。

私のテストでは、 "Loading ..."というテキストを、UIScrollViewの2行分大きい段落に置き換えました。それは正しく表示され、完全にスクロールします。

URLの読み込みが完了すると、正確に同じ段落の段落を2回(文字列の長さの2倍)置き換えます。次に、子のUIViewでsetNeedDisplayを実行します。 UIView(drawRectと同じコードを使用)は新しい境界を取得し、テキストを描画し、親の(UIScrollView)contentSizeを更新します。

エミュレータの画面が更新されると、内容は表示されますが、高さが2倍になるため、テキストの2番目の段落が途切れます。

子供のdrawRectの終わりに子供のフレーム&の境界とともに、親のフレーム、境界、contentSizeなどNSLogを持っています。すべて私に正しいように見えます...子供のフレームと境界は、彼らがそうだったの2倍の高さです、親のcontentSizeはフレームの2倍であり、境界は同じままです。

だから、どこかに図面に影響を与える別のレイヤーが必要ですが、私はそれを見つけることができません。どんな提案も歓迎されます。問題がのdrawRect関数内からビューのサイズを変更することとは何かを持っているようだ、

- (void)drawRect:(CGRect)rect 
{  
    CTTextAlignment paragraphAlignment = kCTLeftTextAlignment; 
    CTParagraphStyleSetting setting[1] = 
    { 
     { kCTParagraphStyleSpecifierAlignment, sizeof(paragraphAlignment), &paragraphAlignment }, 
    }; 

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(setting, 1); 

    NSMutableAttributedString* string = [[[NSMutableAttributedString alloc] initWithString:longText attributes: 
             [NSDictionary dictionaryWithObjectsAndKeys:(id)helvetica, (NSString*)kCTFontAttributeName, 
             paragraphStyle, (NSString*)kCTParagraphStyleAttributeName, nil]] autorelease]; 

    CFRelease(paragraphStyle);  

    [string addAttribute:(id)kCTFontAttributeName 
        value:(id)helvetica 
        range:NSMakeRange(0, [string length])]; 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string); 

    CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, 
                   CFRangeMake(0, 0), 
                   NULL, 
                   CGSizeMake(rect.size.width - 3, CGFLOAT_MAX), 
                   NULL); 

    [self setFrame:CGRectMake(0, 0, (int)(size.width + 0.5), (int)(size.height + 0.5))]; 

    MyContentScroller* scroller = (MyContentScroller*)self.superview; 
    [scroller setContentSize:self.bounds.size]; 

    CGMutablePathRef columnPath = CGPathCreateMutable(); 
    CGRect bounds = CGRectMake(2, 2, self.bounds.size.width - 4, self.bounds.size.height); 
    CGPathAddRect(columnPath, NULL, bounds); 

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, 
               CFRangeMake(0, 0), 
               columnPath, NULL); 
    CGPathRelease(columnPath); 
    CFRelease(framesetter); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
    CGContextTranslateCTM(context, 0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CTFrameDraw(frame, context); 
    CFRelease(frame); 

    CGContextRestoreGState(context); 
} 
+1

関連するコードの一部を表示してください。問題なしで問題を解決することは非常に困難です。 – RyanR

答えて

1

オーケー:

はここに私のコンテンツビューののdrawRect()です。

フレームセッターの初期化を自分の関数にすると、その関数はsetNeedDisplayを呼び出していました。これは、私が持っていた問題を処理していたようです。

-(void)updateText:(NSString*)textStr 
{ 
    if (textStr == longText) { return; } 

    longText = textStr; 

    CTTextAlignment paragraphAlignment = kCTLeftTextAlignment; 
    CTParagraphStyleSetting setting[1] = 
    { 
     { kCTParagraphStyleSpecifierAlignment, sizeof(paragraphAlignment), &paragraphAlignment }, 
    }; 

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(setting, 1); 

    NSMutableAttributedString* string = [[[NSMutableAttributedString alloc] initWithString:longText attributes: 
              [NSDictionary dictionaryWithObjectsAndKeys:(id)helvetica, (NSString*)kCTFontAttributeName, 
              paragraphStyle, (NSString*)kCTParagraphStyleAttributeName, nil]] autorelease]; 

    CFRelease(paragraphStyle);  

    [string addAttribute:(id)kCTFontAttributeName 
        value:(id)helvetica 
        range:NSMakeRange(0, [string length])]; 

    if (framesetter) { CFRelease(framesetter); } 

    framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string); 

    CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, 
                   CFRangeMake(0, 0), 
                   NULL, 
                   CGSizeMake(self.bounds.size.width - 4, CGFLOAT_MAX), 
                   NULL); 

    [self setFrame:CGRectMake(0, 0, (int)(size.width + 0.5), (int)(size.height + 0.5))]; 

    MyContentScroller* scroller = (MyContentScroller*)self.superview; 
    [scroller setContentSize:self.bounds.size]; 

    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect 
{  
    if (!framesetter) { return; } 

    CGMutablePathRef columnPath = CGPathCreateMutable(); 
    CGRect bounds = CGRectMake(2, 2, self.bounds.size.width - 4, self.bounds.size.height); 
    CGPathAddRect(columnPath, NULL, bounds); 

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, 
               CFRangeMake(0, 0), 
               columnPath, NULL); 
    CGPathRelease(columnPath); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
    CGContextTranslateCTM(context, 0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CTFrameDraw(frame, context); 
// CFRelease(frame);  // This line causes a crash! 

    CGContextRestoreGState(context); 
} 
関連する問題