如何在不调整大小的情况下旋转UITextView?

问题描述:

我想在我的VC中旋转一个UITextView。当我尝试旋转它时,UITextView会调整其大小?如何在不调整大小的情况下旋转UITextView?

这是我的代码。

- (void)viewDidLoad { 

     [super viewDidLoad]; 
     self.edgesForExtendedLayout = UIRectEdgeNone; 

     self.title = @"Signature View"; 

     [self.signatureView setLineWidth:2.0]; 
     self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000]; 

     NSLog(@"contentsize: %.0f, %.0f", self.view.frame.size.width, self.view.frame.size.height); <-- this is (contentsize: 320, 568) 

     self.howToTextView.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width); 

     [self.howToTextView setNeedsDisplay]; <-- to make it redraw 

     [self.howToTextView setTransform:CGAffineTransformMakeRotation(-90* M_PI/180)]; 

     NSLog(@"contentsize: %.0f, %.0f", self.howToTextView.contentSize.width, self.howToTextView.contentSize.height); <--- turns out to be (contentsize: 103, 138)same as on storyboard design 

    } 

UPDATE(这段代码的UITextView的到位和框架是正确的,但不是文本不会调整,填补了uitextviews新的帧的大小及其在底角陷了下去。):

- (void)viewDidLoad { 

     [super viewDidLoad]; 
     self.edgesForExtendedLayout = UIRectEdgeNone; 

     self.title = @"Signature View"; 

     [self.signatureView setLineWidth:2.0]; 
     self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000]; 

     NSLog(@"contentsize: %.0f, %.0f", self.view.frame.size.width, self.view.frame.size.height); 

     self.howToTextView = [[UITextView alloc] init]; 
     self.howToTextView.backgroundColor = [UIColor redColor]; 
     self.howToTextView.text = @"this is a good day. i am going to make millions today. Smile while on the phone as it is the best way to increase your business. I hope that you like this app as we have work really hard on it. Please sign your name or decline but if you decline then you understand you lose all coverage."; 

     [self.view addSubview:self.howToTextView]; 


     [self.howToTextView setTransform:CGAffineTransformMakeRotation(-90* M_PI/180)]; 
     self.howToTextView.frame = CGRectMake(0, 0, 80, self.view.frame.size.height); 
     [self.howToTextView setNeedsDisplay]; 

     NSLog(@"contentsize: %.0f, %.0f", self.howToTextView.contentSize.width, self.howToTextView.contentSize.height); 

    } 

更新2:坚持一个UIView内的TextView使得内容大小合适的,但现在有了这个代码的UITextView/UIView的是在应用程序的右上角,即使我将它们都为0 ,0代表x,y?不知道这是为什么?

 self.howToTextView = [[UITextView alloc] init]; 
     [self.howToTextView setFrame:CGRectMake(0, 0, self.view.frame.size.height-20, 110)]; 
     self.howToTextView.backgroundColor = [UIColor redColor]; 
     self.howToTextView.text = @"this is a good day. i am going to make millions today. Smile while on the phone as it is the best way to increase your business. I hope that you like this app as we have work really hard on it. Please sign your name or decline but if you decline then you understand you lose all coverage."; 

     UIView *myRotateView = [[UIView alloc] init]; 
     [myRotateView setFrame:CGRectMake(0, 0, self.view.frame.size.height, 120)]; 
     [myRotateView setBackgroundColor:[UIColor greenColor]]; 
     [myRotateView addSubview:self.howToTextView]; 

     myRotateView.transform = CGAffineTransformMakeRotation(-90* M_PI/180); 
     [[self view] addSubview:myRotateView]; 
+0

如果您使用的是故事板,即使没有转换,您的内容大小也可能会被修改。是这样吗? – 2014-10-09 14:56:25

+0

是的,我从故事板中删除并现在手动创建它。我有它正确的形状和大小,但现在的文字只占UITextView约.10。它像UITextView的外边缘已经正确改变,但内部内容和文本没有意识到它。更新上面的代码。 – jdog 2014-10-09 15:10:20

挣扎后两天,我发现了一个解决办法:

  1. 创建一个UIView。
  2. 将你的UITextView作为子视图添加到它。
  3. 使用sizeThatFits()获取适当大小的UITextView。
  4. 设置UITextView框架。
  5. 设置0高度和0宽度的UIView帧(这很重要!)。
  6. 旋转你的UIView。

现在,如果你想改变字体大小做这样的:

myTextView.font = UIFont.boldSystemFontOfSize(size) 
let maxsize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT) 
let frame = myTextView.sizeThatFits(maxsize) 
myTextView.frame = CGRectMake(0, 0, frame.width, frame.height) 

年末再次旋转的UIView。