UITextView在可见文本的末尾添加...更多按钮

时间:2014-09-02 09:56:43

标签: ios objective-c iphone

UITextView添加...在facebook上可见文字末尾的更多按钮。 如果它已经在某处提到过,我很抱歉。具体来说,我感兴趣的是如何用包装词来计算可见文本结尾的位置

1 个答案:

答案 0 :(得分:0)

1。根据文本和字体检查UItextView大小:

// return the size for UITextView for both IOS7 and IOS6
-(CGSize) getSizeWithMaxLabelSize:(CGSize) maxLabelSize forTextViewWithText:(NSString *) text
{
CGSize expectedLabelSize;
CGSize maximumLabelSize = maxLabelSize;
if (SYSTEM_VERSION_GREATER_THAN(@"6.2")) {
    NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Arial" size:EPCommentViewFontSize] forKey: NSFontAttributeName];
    CGRect rect =[text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil];
    expectedLabelSize = rect.size;
    expectedLabelSize.height = ceilf(expectedLabelSize.height);
    expectedLabelSize.width  = ceilf(expectedLabelSize.width);
} else {
    expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"Arial" size:EPCommentViewFontSize] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
}
  return expectedLabelSize;
}

2。将您的UITextView高度与expectedLabelSize.height进行比较以显示ViewMore或不显示

3。触摸viewMore时更改UITextView frame.size.height

CGRect frame = _textView.frame;
frame.size.height = [self getSizeWithMaxLabelSize:CGSizeMake(_textView.frame.size.height, NSIntegerMax) forTextViewWithText:_textView.text].height;
_textView.frame = frame;