Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView rotation causes content size to be wider than frame width

Basically I have a UITextView and when I rotate it the content size is wider than the frame size. This causes the text view to scroll horizontally, when there is no text outside of the frame. This behavior is not wanted.

UITextView *ingredientsTextView = [[[UITextView alloc] initWithFrame:CGRectMake(50, 100,315,300)] autorelease];
    [ingredientsTextView setBackgroundColor:[UIColor clearColor]];
    [ingredientsTextView setEditable:NO];
    [ingredientsTextView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [ingredientsTextView setFont:[UIFont fontWithName:@"Helvetica" size:18]];
    [ingredientsTextView setText:ingredientsText];
    ingredientsTextView.transform = CGAffineTransformMakeRotation((3.44*M_PI)/180);
    [ingredientsView addSubview:ingredientsTextView];

I can't understand why this is happening. Even putting in a setContentSize method causes it to scroll horizontally (only by about 5/10pts) even when the width is < 100!

Any ideas? Is it just a side effect of rotation?

Thanks Tom

like image 301
Thomas Clayson Avatar asked Sep 11 '25 19:09

Thomas Clayson


2 Answers

I was also stuck with this problem.

The only solution which I found was to create an instance of UIView and add the UITextView as a subview. Then you can rotate the instance of UIView and UITextView will work just fine.

UITextView *myTextView = [[UITextView alloc] init];
[myTextView setFrame:CGRectMake(0, 0, 100, 100)];

UIView *myRotateView = [[UIView alloc] init];
[myRotateView setFrame:CGRectMake(20, 20, 100, 100)];
[myRotateView setBackgroundColor:[UIColor clearColor]];
[myRotateView addSubview:myTextView];

myRotateView.transform = CGAffineTransformMakeRotation(0.8);
[[self view] addSubview:myRotateView];
like image 99
Armands Antans Avatar answered Sep 14 '25 09:09

Armands Antans


I think you should not rotate the textView, what you should rotate is the view under it. For example, the view of navigationController, or the content view of a viewController.

like image 34
AechoLiu Avatar answered Sep 14 '25 08:09

AechoLiu