Я работаю с EGOTextView.I устанавливаю textContentView для viewForZoomingInScrollView.when я уменьшаю его и печатаю. Иногда он возвращает неправильную высоту. Вот что я делаю:CTFramesetterSuggestFrameSizeWithConstraints возвращает неправильную высоту при уменьшении масштаба?
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;
}
Но это, кажется, всегда возвращает неправильную высоту при вводе текста после увеличения. Есть ли другой/лучший способ определить правильную высоту для моей атрибутной строки? Благодаря!