2013-04-11 81 views
0

我與EGOTextView.I設置textContentView for 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; 
} 

但它似乎總是返回一個錯誤的高度,當你放大後輸入文字。 有沒有其他的/更好的方法來找出我的屬性字符串的正確高度?謝謝!

回答

相關問題