UITextView - Horizontal Scrolling?

隐身守侯 提交于 2019-11-27 07:22:39

问题


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

Thanks for your answers.

EDIT: 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);

but I received: 'Threat 1: signal SIGABRT'


回答1:


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

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

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

  3. Set yourScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times

  4. Also set titleTextView.frame = CGRectMake(titleTextView.frame.origin.x, titleTextView.frame.origin.y, textLength, titleTextView.frame.size.height);

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

Hope this gives you a good start!

EDIT: This Code will work:

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


来源:https://stackoverflow.com/questions/15190000/uitextview-horizontal-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!