如何在用户点击“更多”时展开UITextview按钮在ios中 [英] how to expand the UITextview when user clicks on "more " Button in ios

查看:216
本文介绍了如何在用户点击“更多”时展开UITextview按钮在ios中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些应用程序使用UITextview显示给定数量的行,当用户点击更多时,它会展开并显示其余部分,如何添加越来越少的按钮。请给我任何想法。我是编程的新手。我知道如何编写UITextview的代码。但我不知道如何为UITextview.Thanks提前编写更多或更少按钮的代码。

There are applications with a UITextview that shows a given amount of lines and when the user clicks more it expands and shows the rest,how to add more and less button.Please give me any idea. i am new to the programming.I know how to write the code of UITextview.But i dont know how to write the code of More or less button for UITextview.Thanks in advance.

推荐答案

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

1.Check UItextView size base on text and Font:

// 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.Change UITextView frame.size.height

2.Change 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;

3.更改子视图布局

// this will work only if you textview is on the top of other views. 
[UIView animateWithDuration:0.25 animations:^{
        for (UIView *subview in self.view.subviews) 
            subview.transform = CGAffineTransformMakeTranslation(0, _textView.frame.size.height);
}];

应该是它。

这篇关于如何在用户点击“更多”时展开UITextview按钮在ios中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆