2010-04-21 61 views
0

我有一个UILabel:如何在UILabel非中心创建文本?

descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)]; 
    [descriptionLabel setFont:[Utils getSystemFontWithSize:14]]; 
    [descriptionLabel setBackgroundColor:[UIColor clearColor]]; 
    [descriptionLabel setTextColor:[UIColor whiteColor]]; 
    descriptionLabel.numberOfLines = 0; 
    descriptionLabel.lineBreakMode = UILineBreakModeWordWrap; 
    [self addSubview:descriptionLabel]; 

如果文本只有1线长就会出现在标签的中间。

是否有反正我可以从标签的左上角而不是中间中心显示文字?

回答

2

您可以设置

[descriptionLabel setTextAlignment:UITextAlignmentLeft];

或者如果你喜欢点语法

descriptionLabel.textAlignment = UITextAlignmentLeft;

+0

不幸的是,该文本还出现在中间做了这些改变之后。 – 2010-04-21 17:51:31

+1

你的意思是垂直在中间吗?尽管是的,它是左对齐的吗? 您可以在使用NSString的sizeWithFont方法计算后设置标签的高度 - 更多细节:http://stackoverflow.com/questions/455553/what-is-nsstring-sizewithfontforwidthlinebreakmode-good-for – prendio2 2010-04-21 17:56:57

0

左/右对齐集合的UILabel的textAlignment财产要么UITextAlignmentLeftUITextAlignmentRight

更多的微调,您可以覆盖drawTextInRect: