开发者

How can I make text in a UILabel non-centered?

I have a 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 = UILineBreakMode开发者_开发技巧WordWrap;
    [self addSubview:descriptionLabel];

If the text is only 1 line long it will appear in the middle of the label.

Is there anyway I can have the text display from the top left corner of the label instead of the middle center?


You can set

[descriptionLabel setTextAlignment:UITextAlignmentLeft];

or if you prefer dot syntax

descriptionLabel.textAlignment = UITextAlignmentLeft;


for left/right alignment set the textAlignment property of UILabel to either UITextAlignmentLeft or UITextAlignmentRight.

for more fine tuning you can override drawTextInRect:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜