2014-10-09 170 views
1

我想在我的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

回答

0

掙扎後兩天,我發現了一個解決辦法:

  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。

相關問題