I'm interested in creating UITextView
that is expanding dynamically while typing the text, and scaling as the user pinches the screen(Similar behaviour can be found in TinyPost).
When you just type (without pinching) the textView expands fine. When you just pinch (without typing) is works fine, but when you pinch and then type, the text inside gets cut.
Here is my code:
UIPinchGestureRecognizer *pinchGestRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleTextView:)];
pinchGestRecognizer.delegate = self;
[bgFrameImageView addGestureRecognizer:pinchGestRecognizer];
- (void)scaleTextView:(UIPinchGestureRecognizer *)pinchGestRecognizer{
createTextView.transform = CGAffineTransformScale(createTextView.transform, pinchGestRecognizer.scale, pinchGestRecognizer.scale);
pinchGestRecognizer.scale = 1;
}
- (void)textViewDidChange:(UITextView *)textView{
CGSize textSize = textView.contentSize;
textView.frame = CGRectMake(CGRectGetMinX(textView.frame), CGRectGetMinY(textView.frame), textSize.width, textSize.height); //update the size of the textView
}
What do you think?
Not the best but easy solution would be:
I do not know exactly your use case, but I think it does not make sense in many cases to support in a app pinch zoom while typing text. I would just set a flag, which prevents pinch zoom while user is typing text.
If you mean by text typing, editing the text view you can end the editing at the beginning of the pinch gesture and start editing after pinch gesture did end.
UITextView is a subclass of UIScrollView and as such you need to do what you do with any scroll view to enable zooming:
... that zooms the entire text view.
If you only want to zoom the text then you have to perform these steps via a pinch gesture recognizer:
First of all add
UIPinchGestureRecognizer
inviewDidLoad
method:then put this method in your
viewController
to scaletextField
font:Try:
It basically scales the font size and then recalculates the content size using your code in textViewDidChange.
To elaborate on @Cocoanetics answer above. I implemented the gesture handling idea for attributed strings on iOS 7 but it is prohibitively slow when you have too many font changes in your string. There is also a ridiculous buffering bug in iOS 7 where Change notifications keep firing long after you've stopped pinching - reminds me of the stupid keyboard buffer in early versions of PC-DOS. Anyway, I have put the code below that got this working for me - although it has only resulted in informing me that this is a waste of time and that I need to give my users some other way of scaling their fonts.