UITextview
每侧有8个点的填充。因此,我将16点减少到我想要找到的CGRect-高度的宽度。
正如在下面的函数(使用sizeWithFont
)中可以看到的,对于iOS6,我得到了点完美的高度。
但对于iOS7,当我使用该函数时(使用boundingRectWithSize
),我获得的高度并不准确。
#pragma mark - Private methods
- (CGFloat)getTextHeight{
if (isSentMessgae) {
return [_chatMessageModel.message sizeWithFont:[UIFont fontWithName:FONT_TT size:16] constrainedToSize:CGSizeMake(194, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping].height;
}
return [_chatMessageModel.message sizeWithFont:[UIFont fontWithName:FONT_TT size:16] constrainedToSize:CGSizeMake(154, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping].height;
}
- (CGFloat)getTextHeightIOS7{
if (isSentMessgae) {
NSString *text = _chatMessageModel.message;
CGFloat width =154;
UIFont *font = [UIFont fontWithName:FONT_TT size:16];
if (isSentMessgae) {
width =194;
}
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:text
attributes:@
{
NSFontAttributeName: font
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
DebugLog("text heigh : %f",rect.size.height);
return (rect.size.height);
}
NSString *text = _chatMessageModel.message;
CGFloat width =154;
UIFont *font = [UIFont fontWithName:FONT_TT size:16];
if (isSentMessgae) {
width =194;
}
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:text
attributes:@
{
NSFontAttributeName: font
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
// DebugLog("text heigh : %f",rect.size.height);
return (rect.size.height ));
}