2013-03-03 252 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