Dynamic expand UITextView on ios 7

2019-02-03 06:52发布

I am using this code

CGRect frame = self.mytext.frame;
frame.size.height = self.mytext.contentSize.height;
self.mytext.frame = frame;

But it doesn`t work in iOS 7. Does anyone know why or have the same problem?

EDITED:

Sorry. I made an UIView, a UITextView and a UIScrollView so i can load the text in my uitextview expand.

I used this code in viewDidLoad

CGRect frame = self.moreDetailsTextView.frame;
frame.size.height = self.moreDetailsTextView.contentSize.height;
self.moreDetailsTextView.frame = frame;

CGRect moreViewFrame =  self.moreDetailsView.frame;
moreViewFrame.size.height = frame.size.height + 270;
self.moreDetailsView.frame = moreViewFrame;

int scrollViewHeight =  moreViewFrame.size.height + 235;
self.scrollView.contentSize = CGSizeMake(320, scrollViewHeight);

This was working fine in ios6, but now with xcode5 and ios7 the uitextview does not expand when i test the app in the simulator.

9条回答
我命由我不由天
2楼-- · 2019-02-03 06:58
[UITextView sizeToFit];

Using sizeToFit resizes the textview but cuts some text from the bottom. Any idea how to resolve this?

Solution:

Finally the method described at the following thread worked. https://stackoverflow.com/a/18818036/1869452

查看更多
孤傲高冷的网名
3楼-- · 2019-02-03 06:59

I've worked on a complete solution to solve this on iOS 9.3.1. Find it here.

enter image description here

查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-03 07:02

Finally i did it.

If anyone is having the same problem just had this code before

    [_moreDetailsTextView sizeToFit];
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-02-03 07:08

To address this exact problem I made an auto-layout based light-weight UITextView subclass which automatically grows and shrinks based on the size of user input and can be constrained by maximal and minimal height - all without a single line of code.

Made primarily for use in Interface builder and only works with Auto layout

https://github.com/MatejBalantic/MBAutoGrowingTextView

查看更多
三岁会撩人
6楼-- · 2019-02-03 07:16

Recently I have found this CSGrowingTextView that sizes while you type, maybe it can help: https://github.com/cloverstudio/CSGrowingTextView

查看更多
Juvenile、少年°
7楼-- · 2019-02-03 07:16

Get the new size of the textView using this method

CGSize sz = [_textView.text sizeWithFont:_textView.font]

in case that didn't work very well with the height,get the width you just got from the preivous method and use in the next method to get the appropriate height you need

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
    UITextView *textView = [[UITextView alloc] init];

    [textView performSelectorOnMainThread:@selector(setAttributedText:)
                                    withObject:text
                                 waitUntilDone:YES];

    CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}

Note : if there was any animation in the view,all the changes made in the view.frame will be discarded as the view will be redrawn again. So it would be better to deal with the constraints than dealing with the frame itself as the changes you will make in the constraint will be permanent even if the view got refreshed.

create IBoutlet of a constraint and connect it to your class like that

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *your_constraint;

change the value of the contraint just like the way you change the view.frame parameters like that

            _constraintHeaderTitle.constant = THE_NEW_VALUE;
查看更多
登录 后发表回答