首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向UITextView添加换行符"\n“,但不起作用

向UITextView添加换行符"\n“,但不起作用
EN

Stack Overflow用户
提问于 2011-12-09 16:43:00
回答 2查看 974关注 0票数 0

根据主题,我的代码如下

代码语言:javascript
复制
- (void)viewDidLoad
{

    // Add Scroll View
    CGRect fullScreenRect = [[UIScreen mainScreen] applicationFrame];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:fullScreenRect];
    self.view = scrollView;

    // Configure Scroll View
    scrollView.contentSize = CGSizeMake(320, 500);

    // Shop Detail
    CGRect shopDetailFrame = CGRectMake(20, 5, 300, 80);
    UITextView *shopDetail = [[[UILabel alloc] initWithFrame:shopDetailFrame] autorelease];
    shopDetail.backgroundColor = [UIColor whiteColor];
    shopDetail.font = [UIFont boldSystemFontOfSize:15];
    NSString *shopName = @"Shop A, G/F, 1/F-3/F, Lok Sing Building, 16 Kau Yuk Road";
    shopDetail.text = [[shopName stringByAppendingString:@"\n"] stringByAppendingString:shopName];
    [scrollView addSubview:shopDetail];

    [super viewDidLoad];
}

假设输出为:

代码语言:javascript
复制
Shop A, G/F, 1/F-3/F, Lok Sing Building, 16 Kau Yuk Road
Shop A, G/F, 1/F-3/F, Lok Sing Building, 16 Kau Yuk Road

但是我得到了:

代码语言:javascript
复制
Shop A, G/F, 1/F-3/F, Lok Sing Building, 16 Kau Yuk Road...

\n不能在UITextView上工作,有人知道如何在UITextView上换行吗?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-09 16:47:18

就是这一行:

代码语言:javascript
复制
UITextView *shopDetail = [[[UILabel alloc] initWithFrame:shopDetailFrame] autorelease];

您创建的是UILabel而不是UITextView。

您仍然可以使用UILabel,但默认情况下它将只显示一行。要使UILabel显示更多行,需要将numberOfLines属性设置为0 (无限制)或实际需要的行数。

票数 2
EN

Stack Overflow用户

发布于 2015-10-30 01:10:43

如果您以编程方式使用\n,它将正常工作。

代码语言:javascript
复制
var txt_Other_Proj = String()
txt_Other_Proj = "Other Jessit Apps and Projects"
txt_Other_Proj = txt_Other_Proj + "\n"
txt_Other_Proj = txt_Other_Proj + "Hurkle: A FIND ME game"
txt_Other_Proj = txt_Other_Proj + "\n"
txt_Other_Proj = txt_Other_Proj + "A free game."
txt_Other_Proj = txt_Other_Proj + "\n"
txt_Other_Proj = txt_Other_Proj + "Candida Cookbook"
txt_Other_Proj = txt_Other_Proj + "\n"
txt_Other_Proj = txt_Other_Proj + "Living with Candida Albicans"
txt_Other_Proj = txt_Other_Proj + "\n"
txt_Other_Proj = txt_Other_Proj + "Visit www.Jessit.com"

main_Info.text = txt_Other_Proj
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8443013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档