0

私はEGOTextViewで作業しています。ズームアウトして入力すると、viewForZoomingInScrollView.のtextContentViewを設定します。間違った高さを返すことがあります。これが私がやっていることです:

CTFramesetterRef framesetter = _framesetter;
CFAttributedStringRef attributedStringRef=(CFAttributedStringRef)self.attributedString;
_framesetter = CTFramesetterCreateWithAttributedString(attributedStringRef);

if (framesetter!=NULL) {
    CFRelease(framesetter);
}
CGRect rect = self.textContextView.frame;

CGFloat height = [self heightForAttributedString:self.attributedString forWidth:rect.size.width];
rect.size.height = (height + self.font.lineHeight * 2) * pageScale;
//pageScale is the scale scrollview end zooming


- (CGFloat)boundingHeightForWidth:(CGFloat)width {

CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(_framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width , CGFLOAT_MAX), NULL);
return suggestedSize.height;
}

そして、私もこの方法を試します:

+(CGFloat)heightForAttributedString:(NSAttributedString *)attrString forWidth:(CGFloat)inWidth
{

CGFloat H = 0;

// Create the framesetter with the attributed string.
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( (CFMutableAttributedStringRef) attrString);

CGRect box = CGRectMake(0,0, inWidth, CGFLOAT_MAX);

CFIndex startIndex = 0;

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, box);

// Create a frame for this column and draw it.
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(startIndex, 0), path, NULL);

// Start the next frame at the first character not visible in this frame.
//CFRange frameRange = CTFrameGetVisibleStringRange(frame);
//startIndex += frameRange.length;

CFArrayRef lineArray = CTFrameGetLines(frame);
CFIndex j = 0, lineCount = CFArrayGetCount(lineArray);
CGFloat h, ascent, descent, leading;

for (j=0; j < lineCount; j++)
{
    CTLineRef currentLine = (CTLineRef)CFArrayGetValueAtIndex(lineArray, j);
    CTLineGetTypographicBounds(currentLine, &ascent, &descent, &leading);
    h = ascent + descent + leading;
    NSLog(@"%f", h);
    H+=h;
}

CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);


return H;
}

ただし、ズーム後にテキストを入力すると、常に間違った高さが返されるようです。属性付き文字列の正しい高さを把握するための他の/より良い方法はありますか? ありがとう!

4

1 に答える 1

0

私もこのケースで実行し、それを解決して、すべてのケースで機能するカテゴリを作成しました。高さとしてゼロを使用する場合、高さを任意の高さに制限しません。この問題のカテゴリを作成しました。これは、すべての場合に役立ちます。動的テキストと画像が一緒にレンダリングされた iOS UITableView を参照してください (NSAttributedString + 画像)

于 2014-03-09T02:46:40.193 に答える