質問

I have been searching for hours and I can't find any solution.

I want to make a UITextField or UITextView (don't care whichever one works) display the word "Contacts..." in a single line, then when i press a button, the textfield should grow to fit all of the content. Ideally I want to have all of the components on my view move down to accommodate the larger text field.

I have seen this technique in tons of apps but I cannot find a solution! There must be some GitHub library with a UITextField that has this behaviour.

Please help!

役に立ちましたか?

解決

I had a similar need a while back, and made a category to support resizing of each UITextView. Just add this category to your project, import it to the class that you need it in, and call autoAdjustHeight on your UITextView when you want to resize it.

UITextView+Resizeable.h:

@interface UITextView (Resizeable)

-(void)autoAdjustHeight;

@end

UITextView+Resizeable.m:

#import "UITextView+Resizeable.h"

@implementation UITextView (Resizeable)

-(void)autoAdjustHeight {

    CGFloat fixedWidth = self.frame.size.width;
    CGSize newSize = [self sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = self.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    self.frame = newFrame;
    self.scrollEnabled = NO;
}

@end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top