我需要在UIButton的左侧显示一个邮箱地址,但它被放在中心位置.

有没有办法将校准设置为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];

推荐答案

设置contentHorizontalAlignment:

emailBtn.contentHorizontalAlignment = .left;

您可能还需要调整左插图的内容,否则文本将触及左边框:

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

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

Ios相关问答推荐

不使用iOS 17修改器修改SWIFT用户界面视图

如何通过S定义的对象设置删除基于块的观察者的测试?

我应该使用哪个UIButton发送事件在SWIFT上刷卡?

使用AVCaptureEventInteraction捕获音量按键快门

在相机动作之前快速防止场景视点重置

SwiftUI检测顶级和安全区域插图

如何在后台显示 Swift UI 中的通讯通知?

在 Swift 中,对象如何被准确地从内存中移除?

当任何 TextEditor 有多于一行时,所有 TextEditor 都会调整大小

将 URLCache 子类与 URLSession 一起使用

带有属性包装器的不可用属性

通过按钮打开 AppStore

何时需要将应用程序源包含在测试目标中?

如何使用 swift 显示 .svg 图像

为什么 Safari 在使用 iOS 6 设备进行远程调试时显示No Inspectable Applications?

UIView 动画与 CALayers

iOS Swift - 获取当前本地时间和日期时间戳

判断从 JSON 字符串返回的 Objective-C 中的空值

Parse for iOS:try 运行应用程序时出错

在两个或多个 iPhone 应用程序之间共享数据