@
jox 找到三种计算富文本字符串高度的方法,三种方法计算出来的高度是一样的
但与shouldInteractWithTextAttachment(UITextViewDelegate点击富文本里的图片后的回调)输出的UITextView内容高度始终不同,本文最后附上了NSLog记录
ps:如果富文本字符串只包含图片,例如图片是高50的,那UITextView.contentSize.height就是66,图片是高60的,那UITextView.contentSize.height就是76,必然相差了16,求解
再ps:下文三个width参数我明明赋值了320,但NSLog出来只有318.62,求解
- (void)Calculating_Text_Height_1_Width:(CGFloat)width WithString:(NSAttributedString *)string {
NSTextStorage *textStorage = [[NSTextStorage alloc] init];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(width, FLT_MAX)];
[textContainer setLineFragmentPadding:0.0];
[layoutManager addTextContainer:textContainer];
[textStorage setAttributedString:string];
[layoutManager ensureLayoutForTextContainer:textContainer];
CGRect frame = [layoutManager usedRectForTextContainer:textContainer];
NSLog(@"1:%@", NSStringFromCGRect(frame));
/*
http://www.v2ex.com/t/149498 */
}
- (void)Calculating_Text_Height_2_Width:(CGFloat)width WithString:(NSAttributedString *)string {
CGRect frame = [string boundingRectWithSize:CGSizeMake(width, FLT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) context:nil];
NSLog(@"2:%@", NSStringFromCGRect(frame));
/*
http://blog.csdn.net/iunion/article/details/12185077 */
}
- (void)Calculating_Text_Height_3_Width:(CGFloat)width WithString:(NSAttributedString *)string {
NSTextStorage *textStorage = [[NSTextStorage alloc] init];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(width, FLT_MAX)];
[textContainer setLineFragmentPadding:0.0];
[layoutManager addTextContainer:textContainer];
[textStorage setAttributedString:string];
[layoutManager glyphRangeForTextContainer:textContainer];
CGRect frame = [layoutManager usedRectForTextContainer:textContainer];
NSLog(@"3:%@", NSStringFromCGRect(frame));
/*
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html http://www.cocoachina.com/b/?p=160 */
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange {
NSLog(@"img:%@", NSStringFromCGRect(textAttachment.bounds));
NSLog(@"total:%@", NSStringFromCGSize(textView.contentSize));
return true;
}
========
2014-12-05 22:04:05.640 test[3673:112783] View Controller Bounds:{{0, 0}, {320, 568}}
2014-12-05 22:04:05.641 test[3673:112783] UITextView Controller Bounds:{{0, 0}, {600, 600}}
2014-12-05 22:04:05.671 test[3673:112783] 1:{{0, 0}, {318.62, 637.36000000000001}}
2014-12-05 22:04:05.672 test[3673:112783] 2:{{0, 0}, {318.62, 637.36000000000001}}
2014-12-05 22:04:05.673 test[3673:112783] 3:{{0, 0}, {318.62, 637.36000000000001}}
2014-12-05 22:04:08.612 test[3673:112783] img:{{0, 0}, {50, 50}}
2014-12-05 22:04:08.612 test[3673:112783] total:{320, 664}
===========