有没有办法让iOS7中的UITextview(对于tableview中的聊天单元格)具有恒定宽度的文本高度(点完美而非近似)?

时间:2014-05-15 12:13:33

标签: ios objective-c ios7 uitextview

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 ));
}

0 个答案:

没有答案