UITextView - 水平滚动?

3
我该如何创建一个水平滚动的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个回答

7
我还没有做过这样的事情,但我会尝试以下步骤来完成:
  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); //或者一些您喜欢的值,您可能需要尝试几次

  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];

在将“yourScrollView”添加为子视图后,我收到了“Threat 1: signal SIGABRT”的错误信号。 - Lorenz Wöhr
请更新您的问题并提供一些代码,以便人们可以看到您已经尝试了什么。 - pmk
你的代码肯定出了问题。我已经解决了,可以看看我上面的回答。 - pmk
尝试了很多UITextView的方法,最终只能采用这种方式。 - Prat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接