gpt4 book ai didi

iphone - UITextView - 水平滚动?

转载 作者:可可西里 更新时间:2023-11-01 17:09:08 24 4
gpt4 key购买 nike

如何创建一个水平滚动的 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. 创建一个 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 的 subview :[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];

关于iphone - UITextView - 水平滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15190000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com