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