我需要在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];
在Swift 5.0和Xcode 10.2中
有两种方法
1)直接方法
btn.contentHorizontalAlignment = .left
2) SharedClass示例(编写一次,然后使用每个ware)
这是您的共享类(像这样您可以访问所有组件属性)
import UIKit
class SharedClass: NSObject {
static let sharedInstance = SharedClass()
private override init() {
}
}
//UIButton extension
extension UIButton {
func btnProperties() {
contentHorizontalAlignment = .left
}
}
在你的ViewController调用中
button.btnProperties()//This is your button
如果你使用按钮。contenttedgeinsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10),你会得到一个警告,声明“contenttedgeinsets”在iOS 15.0中已弃用:使用UIButtonConfiguration时忽略此属性。另一种解决方案是:
15、0 +。
var button: UIButton = {
let button = UIButton(configuration: .filled())
button.configuration?.contentInsets = NSDirectionalEdgeInsets(top: 16, leading: 20, bottom: 16, trailing: 20)
button.contentHorizontalAlignment = .leading
return button
}()
在Swift 5.0和Xcode 10.2中
有两种方法
1)直接方法
btn.contentHorizontalAlignment = .left
2) SharedClass示例(编写一次,然后使用每个ware)
这是您的共享类(像这样您可以访问所有组件属性)
import UIKit
class SharedClass: NSObject {
static let sharedInstance = SharedClass()
private override init() {
}
}
//UIButton extension
extension UIButton {
func btnProperties() {
contentHorizontalAlignment = .left
}
}
在你的ViewController调用中
button.btnProperties()//This is your button