我须要从UIButton
左侧显示电子邮件地址,可是它位于中间。 html
有什么办法能够将对齐方式设置到UIButton
的左侧? ide
这是我当前的代码: ui
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];
此处说明了如何执行操做以及其工做原理: http : //cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html spa
若是您不想更改按钮内的整个内容位置,则使用emailBtn.titleEdgeInsets
优于contentEdgeInsets
。 code
若是您不想在代码中进行调整,也可使用界面生成器。 在这里,我使文本左对齐并缩进一些: orm
不要忘记,您也能够在按钮中对齐图像。 htm
UIButton *btn; btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
设置contentHorizontalAlignment: blog
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);