Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel Center contents

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: alt text

like image 840
Linuxmint Avatar asked Dec 16 '10 05:12

Linuxmint


People also ask

How do you center a label in view?

Use NSTextAlignmentCenter if what you have is the label already set and you want to center its content. This is the best answer, having the label frame is centered is not enough for centering the text, very useful if your labels are localized!

What is UILabel?

A view that displays one or more lines of informational text.

How do I center text in a label in Swift?

You can also change the label text alignment by change the UILabel object's textAlignment property value t0 NSTextAlignment. left , NSTextAlignment. center or NSTextAlignment. right .


1 Answers

myLabel.textAlignment=UITextAlignmentCenter;
like image 147
arunkumar.p Avatar answered Sep 21 '22 23:09

arunkumar.p