初试Swift---UIButton

classViewController:UIViewController{

overridefuncviewDidLoad() {

super.viewDidLoad()

//button初始化

letaddButton=UIButton.init(type: .contactAdd)

addButton.frame=CGRect(x:10,y:150,width:100,height:30)

addButton.addTarget(self, action:#selector(tapped), for: .touchUpInside)

self.view.addSubview(addButton)

letcustomButton =UIButton(frame:CGRect(x:10,y:190,width:100,height:30))

customButton.setTitle("好长的字咋办呢你说咋办", for: .normal)

customButton.setTitleColor(UIColor.red, for: .normal)

customButton.titleLabel?.font=UIFont.systemFont(ofSize:12)

customButton.backgroundColor=UIColor.blue

customButton.addTarget(self, action:#selector(touchEd(_:)), for: .touchUpInside)

/*

byTruncatingTail:省略头部文字,省略部分用...代替

byTruncatingMiddle:省略中间部分文字,省略部分用...代替(默认)

byTruncatingTail:省略尾部文字,省略部分用...代替

byClipping:直接将多余的部分截断

byWordWrapping:自动换行(按词拆分)

byCharWrapping:自动换行(按字符拆分)

*/

customButton.titleLabel?.lineBreakMode= .byTruncatingTail

self.view.addSubview(customButton)

// Do any additional setup after loading the view, typically from a nib.

}

functapped(){

print("tapped")

}

functouchEd(_button:UIButton){

print(button.title(for: .normal) ??"111")

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容