Hello I want to make UIButton
title label as a circle. So I did in this way.
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
But it doesn't make it as a circle. It looks like this code doen't affect on the titleLabel
UPDATE
Please help me Thanks
好首先,您必须验证uibutton的clipsToBounds true,但如果已经设置并且问题仍然存在,那么您可能要尝试在viewDidLoad中实现此功能(应该),但出于某些原因,在viewDidLoad布局中的xcode 8版本仍然不是最终版本(对象尚未设置其真实尺寸),如果发现一些不连贯的问题,这可以通过在分配角半径之前记录uibutton框架来验证,如果这是问题,那么您只需要将代码移至viewWillAppear
You are missing to set MasToBounds
btnAttachments.titleLabel?.layer.masksToBounds = true;
add this you code will work fine ,
You should set corner redius in viewDidLayoutSubViews
or drawRect
(usually in UIView
class)
i hope this will help u
Use the following code to get desired result:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.