的标题设置为左对齐

的标题设置为左对齐

本文介绍了如何将 UIButton 的标题设置为左对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 UIButton 的左侧显示一个电子邮件地址,但它被放置在中间.

I need to display an email address on the left side of a UIButton, but it is being positioned to the centre.

有什么办法可以将对齐设置为 UIButton 的左侧?

Is there any way to set the alignment to the left side of a UIButton?

这是我当前的代码:

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];

推荐答案

设置 contentHorizo​​ntalAlignment:

Set the contentHorizontalAlignment:

emailBtn.contentHorizontalAlignment = .left;

您可能还想调整左内嵌的内容,否则文本将触及左边框:

You might also want to adjust the content left inset otherwise the text will touch the left border:

emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);

这篇关于如何将 UIButton 的标题设置为左对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:17