UILabel Align Text to center

Jak wyrównać tekst w UILabel?

Author: Bartłomiej Semańczyk, 2011-04-20

9 answers

Z iOS 6 i Później UITextAlignment jest przestarzały. użyj NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Wersja Swift z iOS 6 i nowszych

myLabel.textAlignment = .center
 546
Author: Aravindhan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-30 11:58:35

Oto przykładowy kod pokazujący, jak wyrównać tekst za pomocą UILabel:

label = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 12)];
label.textAlignment = NSTextAlignmentCenter;

Możesz przeczytać więcej na ten temat tutaj UILabel

 81
Author: Vishal Shah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-28 19:03:44

Aby wyśrodkować tekst w etykiecie UILabel w języku Swift (który jest przeznaczony dla iOS 7+), możesz zrobić:

myUILabel.textAlignment = .Center

Lub

myUILabel.textAlignment = NSTextAlignment.Center
 11
Author: AdamT,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-24 04:34:43

Uwaga: zgodnie z klasą UILabel, od iOS 6 podejście to jest obecnie przestarzałe.

Wystarczy użyć właściwości textAlignment, aby zobaczyć wymagane wyrównanie przy użyciu jednej z wartości UITextAlignment. (UITextAlignmentLeft, UITextAlignmentCenter lub UITextAlignmentRight.)

E. g.: [myUILabel setTextAlignment:UITextAlignmentCenter];

Zobacz odniesienie do klasy UILabel aby uzyskać więcej informacji.

 8
Author: John Parker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-01 09:31:48

Użyj yourLabel.textAlignment = NSTextAlignmentCenter; dla iOS >= 6.0 i yourLabel.textAlignment = UITextAlignmentCenter; dla iOS

 6
Author: Jayprakash Dubey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-12 23:25:33

Dla Swift 3 to:

label.textAlignment = .center
 5
Author: Michał Kwiecień,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-19 09:27:38

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

 4
Author: Rinju Jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-19 07:25:01
Label.textAlignment = NSTextAlignmentCenter;
 3
Author: Mubin Shaikh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-18 10:26:10

W xamarin ios Załóżmy, że nazwa Twojej etykiety to tytuł, a następnie wykonaj następujące czynności

title.TextAlignment = UITextAlignment.Center;
 2
Author: Dilip Jangid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-16 07:11:25