2016-11-19 60 views
0

我想设置一个可点击的“textonly”按钮,但现在我有问题修复我的按钮的高度。在此之前,我试图将它作为标签,但后来触摸操作变得复杂,所以我决定只是做一个没有框架的按钮等。 所以现在我需要设置buttonheight到文本里面,任何想法? 继承人的代码片段:用代码调整UIButton高度?

//... 
    button.setTitle("Log in", for: .normal) 
    button.setTitleColor(.white, for: .normal) 
    button.layer.borderWidth = 0 
    button.frame = CGRect 
    button.addTarget(self, action: #selector(LoginFunction), for: .touchUpInside) 
    //... 

回答

0

您应该仍然有按钮的框架,以显示在用户界面。它定义你的按钮的位置和大小(包括高度)。下面是与位置(0,0),大小(100,100)按钮的例子:

button.frame = CGRect(x: 0, y: 0, width: 100, height: 100) 

也许你所说的“框架”的意思是“边界”。通过将borderWidth设置为0,您可以实现无边框按钮,您已在代码中完成该操作。

button.layer.borderWidth = 0 

如果你想为你能做的文本设置大小:

button.titleLabel?.font = UIFont.systemFont(ofSize: 17) 
1

如果你想在你UIButton你应该设置yourButton.titleLabel?.numberOfLines = 0yourButton.titleLabel?.lineBreakMode = .byWordWrapping多行文本。您将获得多行按钮,您可以根据需要配置哪个高度。 你也计算你应该使用的文字框架

let context = NSStringDrawingContext() 
let frame = yourText.boundingRectWithSize(
    CGSize(width: yourButtonWidth, height: 9999), 
    options: NSStringDrawingOptions.UsesLineFragmentOrigin, 
    attributes: dictionaryOfYourTextAttributes, context: context)