Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you programmatically center the alignment of a text label for iOS?

Tags:

ios

uilabel

I want to set the alignment of a text label, how can I do that?

like image 1000
nari Avatar asked Apr 16 '11 12:04

nari


People also ask

How do I center a label in Xcode?

click on Align button on bottom toolbar in IB then select Horizontal or Vertical center. First you might want to fix width and height as well, if you want to preserve size.

How to align text in label in swift?

You can change label text, label text color, label background color, label border width, label border color by setting it's properties. You can also change the label text alignment by change the UILabel object's textAlignment property value t0 NSTextAlignment. left , NSTextAlignment. center or NSTextAlignment.


1 Answers

I think there are the answers who helped you out. The correct way to do this is:

yourLabelName.textAlignment = NSTextAlignmentCenter; 

for more documentation you can read this: https://developer.apple.com/documentation/uikit/uilabel

In Swift :-

yourLabelName.textAlignment = .center 

Here .center is NSTextAlignment.center

like image 195
Kundan Avatar answered Oct 03 '22 17:10

Kundan