使用代码调整UIButton高度的大小?

时间:2016-11-19 13:35:48

标签: ios swift uibutton height

我正在尝试设置一个可点击的" textonly" -button但是现在我在修复按钮的高度时遇到了问题。在此之前,我尝试将其作为标签,但随后触摸操作变得复杂,所以我决定只做一个没有框架的按钮等。 所以现在我需要将buttonheight设置为Text里面的内容,任何想法? 下面是代码片段:

 //...
    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)
    //...

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

您仍然应该有一个框架,以便在UI中显示按钮。它定义您的按钮位置和大小(包括高度)。以下是位置(0,0)和大小(100,100)的按钮示例:

$url = 'http://mangafox.me/manga/world_trigger/v01/c001/2.html';
$html = file_get_html($url);
echo $html->plaintext;

也许“框架”的意思是“边框”。您可以通过将borderWidth设置为0来实现无边框按钮,这已经在代码中完成了。

   $ch = curl_init("http://mangafox.me/manga/world_trigger/v01/c001/2.html");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    $content = curl_exec($ch);
    curl_close($ch);

    echo $content;

如果要为文本设置大小,可以执行以下操作:

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