2013-03-03 286 views
3

如何創建滾動UITextView的橫向UITextView - 水平滾動?

當我把我的文本編程它增加了自動換行,所以我只能垂直滾動...

titleView.text = @"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text."; 

謝謝您的回答。

編輯: 到目前爲止,我嘗試這樣做:

UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 ,  self.view.frame.size.width, 50)]; 
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width; 

yourScrollview.contentSize = CGSizeMake(textLength + 200, 500); //or some value you like, you may have to try this out a few times 

titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height); 

[yourScrollview addSubview: titleView]; 

NSLog(@"%f", textLength); 

,但我收到:「威脅1:信號SIGABRT」

回答

6

我還沒有做過這樣的事情,但我會嘗試以下步驟來實現:

  1. 創建UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)]; //

  2. 使用CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width; 讓你的文字

  3. 設置的最終長度yourScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times

  4. 還設置titleTextView.frame = CGRectMake(titleTextView.frame.origin.x, titleTextView.frame.origin.y, textLength, titleTextView.frame.size.height);

  5. 製作titleview的yourScrollView的子視圖:[yourScrollView addSubview: titleView];

希望這給你一個好的開始!

編輯:此代碼將工作:

請注意我用了一個UILabel代替UITextView的。

UILabel *titleView   = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)]; 
    titleView.text    = @"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text."; 
    titleView.font    = [UIFont systemFontOfSize:18]; 
    titleView.backgroundColor = [UIColor clearColor]; 
    titleView.numberOfLines  = 1; 

    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)]; 
    CGFloat textLength   = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width; 
    myScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times 

    titleView.frame    = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height); 

    [myScrollView addSubview: titleView]; 
    [self.view addSubview:myScrollView]; 
    [titleView release]; 
    [myScrollView release]; 
+0

加入「yourScrollView」子視圖我得到「威脅1:信號SIGABRT」後 – 2013-03-03 22:02:25

+0

請更新您的問題,並提供一些代碼,以便人們可以看到你已經嘗試過什麼到目前爲止 – pmk 2013-03-03 22:04:16

+0

我更新了我的問題 – 2013-03-03 22:13:07