2012-05-12 10 views
0

私はこれに答えてスタック全体を検索してきましたが、トピックにはたくさんの質問があります。コア・テキストに合わせてUIViewとCGPathを自動拡張する

私はストーリーボード(プロトタイプセル内)で固定サイズのカスタムUIViewを持っています。 UIViewをサブクラス化し、drawRectメソッドを上書きしました。それは基本的にフォーマットされた文字列を一緒に置き、その後、私はそうのようにそれを描く:

罰金だと動作し、私は十分に大きいデフォルトサイズを作れば、それは複数行のテキストを処理します
// now for the actual drawing 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetShadowWithColor(context, 
          CGSizeMake(0, 1), 
          0, 
          [UIColor whiteColor].CGColor); 

CGMutablePathRef path = CGPathCreateMutable(); //1 
CGPathAddRect(path, NULL, self.bounds); 

// flip the coordinate system 
CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
CGContextTranslateCTM(context, 0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 

CTFramesetterRef framesetter = 
CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)stringToDraw); //3 

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


CTFrameDraw(frame, context); //4 

CGPathはすべてのUIViewを使用します。

私はCGPathの幅を固定したままにしたいが、本質的に無制限のテキストに対応するために高さを拡大したいが、今はちょうどカットオフになる(パス/ビューが十分に大きくないため)

CTFramesetterSuggestFrameSizeWithConstraintsを使用して試してみました。誰かが私が何をする必要があるかを達成するためのコードを開発するのを手伝ってもらえますか?描画する文字列と使用するフォントを知る

答えて

0

、あなたは常にあなたが高さを取得し、他の場所でそれを再利用することができますが

CGSize boundingSize = CGSizeMake(self.bounds.size.width, CGFLOAT_MAX); 
CGSize requiredSize = [yourText sizeWithFont:yourFont 
          constrainedToSize:boundingSize 
           lineBreakMode:UILineBreakModeWordWrap]; 
CGFloat requiredHeight = requiredSize.height; 

から境界を得ることができます...ここで

+0

ありがとう@nicolasthenoz私がフィットするよう拡張する必要がある他の理由はありましたが、テキストの直後/下に他の要素を配置する予定だったからです。また、私のテーブルのセルも、テキストのために必要以上に大きく拡大します。 –

+0

'sizeWithFont'はCoreTextでは正確に動作しません。 –

0

は何ですCoreText APIを使用し、sizeWithFontの答えが間違っているのとは違った適切な高さを返すことに注意してください。

// Measure the height required to display the attr string given a known width. 
// This logic returns a height without an upper bound. Not thread safe! 

- (NSUInteger) measureHeightForWidth:(NSUInteger)width 
{ 
    NSAssert(self.isDoneAppendingText == TRUE, @"isDoneAppendingText"); 

    NSAssert(self.attrString, @"attrString"); 

    CFMutableAttributedStringRef attrString = self.attrString; 
    CFRange stringRange = self.stringRange; 

    CGFloat measuredHeight = 1.0f; 

    // Create the framesetter with the attributed string. 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString); 

    if (framesetter) { 
    CFRange fitRange; 
    CGSize constraints = CGSizeMake(width, CGFLOAT_MAX); // width, height : CGFLOAT_MAX indicates unconstrained 

    CGSize fontMeasureFrameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, stringRange, (CFDictionaryRef)NULL, constraints, &fitRange); 

    // Note that fitRange is ignored here, we only care about the measured height 

    measuredHeight = fontMeasureFrameSize.height; 

    CFRelease(framesetter); 
    } 

    return (NSUInteger) ceil(measuredHeight); 
} 
関連する問題