将换行符“\ n”添加到UITextView但不起作用

时间:2011-12-09 08:43:00

标签: objective-c

根据主题,我的代码如下

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

假设输出为:

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

但我得到了:

Shop A, G/F, 1/F-3/F, Lok Sing Building, 16 Kau Yuk Road...

\n无法在UITextView上运行,任何人都知道如何在UITextView上进行换行?

由于

2 个答案:

答案 0 :(得分:2)

就是这一行:

UITextView *shopDetail = [[[UILabel alloc] initWithFrame:shopDetailFrame] autorelease];

您正在创建UILabel而不是UITextView。

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

答案 1 :(得分:0)

如果以编程方式使用\n,则可行。

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