如何將UIButton的標題設置爲左對齊?

我須要從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];

#1樓

此處說明了如何執行操做以及其工做原理: http : //cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html spa


#2樓

若是您不想更改按鈕內的整個內容位置,則使用emailBtn.titleEdgeInsets優於contentEdgeInsetscode


#3樓

若是您不想在代碼中進行調整,也可使用界面生成器。 在這裏,我使文本左對齊並縮進一些: orm

IB中的UIButton

不要忘記,您也能夠在按鈕中對齊圖像。 htm

在此處輸入圖片說明


#4樓

UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

#5樓

設置contentHorizo​​ntalAlignment: 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);
相關文章
相關標籤/搜索