UITextView – 水平滚动?

我怎样才能创build一个水平滚动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'

我还没有做这样的事情,但我会尝试以下步骤来完成这一点:

  1. 创build一个UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)]; // 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 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成为您的ScrollView的子视图: [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];