UITextView - 水平滚动? [英] UITextView - Horizontal Scrolling?

查看:119
本文介绍了UITextView - 水平滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建水平滚动UITextView?

How can I create a horizontal scrolling UITextView?

当我以编程方式设置文本时,它会添加自动换行符,所以我只能垂直滚动...

When I set my text programmatically it adds automatic line breaks, so I can only scroll vertically...

 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.";

感谢您的回答。

编辑:
到目前为止我试过这个:

So far I tried this:

 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'

but I received: 'Threat 1: signal SIGABRT'

推荐答案

我还没有这样做过,但我会尝试以下步骤来实现这个目标:

I have not yet done something like this, but I would try the following steps to accomplish this:


  1. 创建 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;
获取文本的最终长度

Use CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width; to get the final length of your text

设置 yourScrollView.contentSize = CGSizeMake(textLength + 20,50); //或者你喜欢的一些价值,你可能需要尝试几次

同时设置 titleTextView.frame = CGRectMake(titleTextView.frame.origin.x,titleTextView.frame.origin.y,textLength,titleTextView.frame.size.height);

使titleView成为yourScrollView的子视图: [yourScrollView addSubview:titleView];

Make titleView a subview of yourScrollView: [yourScrollView addSubview: titleView];

希望这会给你一个良好的开端!

Hope this gives you a good start!

编辑:此代码将起作用:

This Code will work:

请注意我使用了 UILabel 而不是 UITextView

Please notice I used a UILabel instead of a 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];

这篇关于UITextView - 水平滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆