2017-07-03 66 views
0

你好,我想把UIButton標題標籤做成一個圓圈。所以我這樣做了。UIButton titleLabel作爲圓圈

btnAttachments.setTitle(strcount, for: .normal) 
btnAttachments.setTitleColor(UIColor.white, for: .normal) 
btnAttachments.titleLabel?.backgroundColor=com.getRedcolor() 
btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15) 
btnAttachments.titleLabel?.layer.cornerRadius=(btnAttachments.titleLabel?.frame.width)!/2 

但它並沒有使它成爲一個圓圈。它看起來像這樣的代碼doen't在titleLabel

UPDATE影響

這是我想要 enter image description here

請幫我 感謝

+0

我想你忘記了btnAttachments.titleLabel.clipsToBounds = YES –

+0

我沒有和仍然相同: ( 這是爲什麼? – user1960169

+0

我認爲btnAttachments.titleLabel?.frame.width = 0.我嘗試btnAttachments.titleLabel?.layer.cornerRadius = 10.它的工作 –

回答

0

好首先你要什麼確認uibutton的clipsToBounds爲true,但如果已經設置好了,問題仍然存在,那麼你可能試圖在「應該」工作的viewDidLoad中實現這一點,但對於某些原因兒子在viewDidLoad中的一些xcode 8版本中佈局仍然不是最終的(對象沒有設置其真實尺寸),您可以通過在分配角半徑之前記錄uibutton框架來驗證此情況,如果發現某些不連貫的情況可能是這個問題,櫃面這是問題,那麼你只需要移動你的代碼viewWillAppear中

0

你缺少設置MasToBounds

btnAttachments.titleLabel?.layer.masksToBounds = true; 

添加這個,你的代碼將正常工作,

你應在0​​或drawRect(通常爲UIView類)

我希望這將幫助ü

0

使用下面的代碼來獲得期望的結果:

let btnAttachments = UIButton(type: .custom) 
btnAttachments.setTitle(strcount, for: .normal).setTitleColor(UIColor.white, for: .normal) 
btnAttachments.frame = CGRect(x: 160, y: 100, width: 50, height: 50) 
btnAttachments.titleLabel?.backgroundColor=com.getRedcolor() 
btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15) 
btnAttachments.layer.cornerRadius = 0.5 * button.bounds.size.width 
btnAttachments.clipsToBounds = true