Jak ustawić tytuł UIButton jako wyrównanie do lewej?

Muszę wyświetlić adres e-mail z lewej strony UIButton, ale jest on umieszczony w środku.

Czy Jest jakiś sposób, aby ustawić wyrównanie do lewej strony UIButton?

To jest mój obecny kod:

UIButton* emailBtn = [[UIButton alloc] initWithFrame:CGRectMake(5,30,250,height+15)];
emailBtn.backgroundColor = [UIColor clearColor];
[emailBtn setTitle:obj2.customerEmail forState:UIControlStateNormal];
emailBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
[emailBtn setTitleColor:[[[UIColor alloc]initWithRed:0.121 green:0.472 blue:0.823 alpha:1]autorelease] forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailAction:) forControlEvents:UIControlEventTouchUpInside];
[elementView addSubview:emailBtn];
[emailBtn release];
Author: Jack, 2010-05-04

10 answers

Ustaw zawartość:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

Możesz również dostosować zawartość lewej wstawki, w przeciwnym razie tekst dotknie lewej krawędzi:

emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
 1440
Author: ,
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
2010-05-04 12:22:49

Możesz również użyć kreatora interfejsu, jeśli nie chcesz wprowadzać korekt w kodzie. Tutaj zostawiłam wyrównanie tekstu, a także wcięcie go trochę:

UIButton in IB

Nie zapomnij, że możesz również wyrównać obraz w przycisku.:

Tutaj wpisz opis obrazka

 114
Author: n8tr,
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-03-29 00:13:15

W Języku Swift 3+:

button.contentHorizontalAlignment = .left
 26
Author: Vincent,
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-10-09 07:59:22
UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
 13
Author: bhavik,
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-30 04:58:05

Użycie emailBtn.titleEdgeInsets jest lepsze niż contentEdgeInsets, jeśli nie chcesz zmieniać pozycji całej zawartości wewnątrz przycisku.

 8
Author: Gabriel,
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-08-30 05:01:35

Tutaj jest wyjaśnione, jak to zrobić i dlaczego tak działa: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html

 7
Author: emkaka,
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-03-06 17:02:52

Swift 4+

button.contentHorizontalAlignment = .left
button.contentVerticalAlignment = .top
button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
 6
Author: Zaid Pathan,
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-09-22 11:37:28

Jest mały błąd w kodzie @ DyingCactus. Oto poprawne rozwiązanie, aby dodać etykietę UILabel do UIButton, aby wyrównać tekst przycisku, aby lepiej kontrolować przycisk "title":

NSString *myLabelText = @"Hello World";
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

// position in the parent view and set the size of the button
myButton.frame = CGRectMake(myX, myY, myWidth, myHeight); 

CGRect myButtonRect = myButton.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame: myButtonRect];   
myLabel.text = myLabelText;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor]; 
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];   
myLabel.textAlignment = UITextAlignmentLeft;

[myButton addSubview:myLabel];
[myLabel release];
Mam nadzieję, że to pomoże....

Al

 4
Author: ladhani,
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
2011-01-03 22:59:43

W Xcode 8.2.1 w interface builder przenosi się do:Tutaj wpisz opis obrazka

 4
Author: dang,
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-01-27 12:55:58

Dla Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  
To może pomóc w razie potrzeby.
 2
Author: Suraj Sonawane,
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
2015-12-02 12:31:39