如何在UITextView中粘贴长文本

4

我有一些静态文本数据,想把它们粘贴到UITextView中,但是当我直接粘贴到textView.text时会显示错误。该如何处理呢?请指导。谢谢。

txtView.txt = @"About us

myventr.co is a web-based tool for event creation, event searching and online ticket      selling whilst also serving as a social media network. myventr.co is not just about creating events and buying tickets online but is also a place for you to socialise. You can create your own profile, connect with friends or create your own group for your organisation!

How is this different to other social media websites then? Myevent is about connecting people’s social lives and events all at the same place.  myventr.co will help find all the event details around your location and will also help avoid events clashing by simply searching the Event Calendar before creating an event.";

以下是我尝试的方法。

@HinataHyuga 在程序中出现了意外情况,缺少终止字符。 - iPhone 7
你遇到了哪种类型的错误? :) - Rushabh
你在那里输入新行吗? - Baby Groot
你到底遇到了哪个错误?能否请你解释一下? - Divyam shukla
你在"关于我们"后面是按回车键还是用空格键输入空格,因为如果你使用空格,它不会出现任何错误。 - user1548843
显示剩余2条评论
8个回答

5

不要在下一行使用文本,而应使用'\n'进行换行。


4

如果你想要在不同的行上粘贴文本,你需要使用 \n 自己设置换行。

例如:

@"This is line1\nThis is line 2"

4
如果您想通过编程设置它,您需要在一行中设置它:
txtView.text = @"About us\n\nmyevent.co is a web-based tool for event creation, event searching and online ticket selling whilst also serving as a social media network. myevent.co is not just about creating events and buying tickets online but is also a place for you to socialise. You can create your own profile, connect with friends or create your own group for your organisation!\n\nHow is this different to other social media websites then? Myevent is about connecting people’s social lives and events all at the same place.  myevent.co will help find all the event details around your location and will also help avoid events clashing by simply searching the Event Calendar before creating an event.";

1
添加属性文件 'about.plist'。
NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"about" ofType:@"plist"]];
NSLog(@"%@",dic);

Jain,你能告诉我如何添加到plist吗?我正在尝试通过将文本文件添加到项目中的方式来实现。如何在plist中添加? - iPhone 7
  1. 在左侧窗格中右键单击文件,然后选择“新建文件…”选项。
  2. 从OS X选项卡中选择资源。
  3. 提供一个属性列表选项。
  4. 选择并命名合适的名称。
这将被添加到您的项目中。
- Rinju Jain

1
如果您想将文本分成多行,可以在每行末尾添加一个\字符。
txtView.txt = @"About us\

myevent.co is a web-based tool for event creation, event searching and online ticket      selling whilst also serving as a social media network. myevent.co is not just about creating events and buying tickets online but is also a place for you to socialise. You can create your own profile, connect with friends or create your own group for your organisation!\

How is this different to other social media websites then? Myevent is about connecting people’s social lives and events all at the same place.  myevent.co will help find all the event details around your location and will also help avoid events clashing by simply searching the Event Calendar before creating an event.";

1
尝试:
txtView.txt = @"About us\n myevent.co is a web-based tool for event creation, event searching and online ticket selling whilst also serving as a social media network. myevent.co is not just about creating events and buying tickets online but is also a place for you to socialise. You can create your own profile, connect with friends or create your own group for your organisation! \nHow is this different to other social media websites then? Myevent is about connecting people’s social lives and events all at the same place.  myevent.co will help find all the event details around your location and will also help avoid events clashing by simply searching the Event Calendar before creating an event.";

你遇到的错误是由于你在字符串中输入了回车换行符。因此,编译器无法理解语句的结尾。

1

直接在XIB中设置。这样可以自动处理换行的文本。


1

我尝试了你的代码,发现你提供的输入字符串中需要空格的地方使用新的(\n)换行字符,在需要空格的地方使用其他答案建议的空格。我按照你的代码替换回车符为空格,它可以工作。建议在需要新行时使用换行符。

UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(5, 50, 320, 300)];
textView.text = @"About us                                                                                         myventr.co is a web-based tool for event creation, event searching and online ticket      selling whilst also serving as a social media network. myventr.co is not just about creating events and buying tickets online but is also a place for you to socialise. You can create your own profile, connect with friends or create your own group for your organisation!                                                                                                        How is this different to other social media websites then? Myevent is about connecting people’s social lives and events all at the same place.  myventr.co will help find all the event details around your location and will also help avoid events clashing by simply searching the Event Calendar before creating an event."; 
[self.view addSubview:textView];

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