UILabel Center contents

柔情痞子 提交于 2019-12-07 01:16:43

问题


Here's my code:

UILabel *myLabel;
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 480)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines
myLabel.text = @"Please select at least one image category!";
myLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size: 24.0];
myLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
myLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:myLabel];
[myLabel release];

I was wondering if it is possible to center the wrapped text as it is alined to the left side:


回答1:


myLabel.textAlignment=UITextAlignmentCenter;



回答2:


UITextAlignmentCenter was the correct method to center the text, but is now depreciated. - see this question and answer.

Use NSTextAlignmentCenter instead:

label.textAlignment = NSTextAlignmentCenter;

This was mentioned in a comment, just making it clear for rookies like myself.




回答3:


Use this:

myLabel.textAlignment = NSTextAlignmentCenter;

UITextAlignmentCenter is deprecated from iOS 6!



来源:https://stackoverflow.com/questions/4457779/uilabel-center-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!