如何根据Button的标题将autoSize设置为UI按钮?

时间:2017-07-18 08:56:09

标签: ios swift uibutton

我有按钮。为获取按钮的标题,我向服务器发送请求,然后设置Button的标题。现在我需要将自动调整大小设置为此按钮。这该怎么做 ?我在按钮的身份检查器中设置了Line Break中的CharacterWrap。现在文本以多行显示,但我还需要调整按钮的大小,因为有些文本太大而有些文本太小。这是它在Storyboard中的样子和带约束的约束一切正常我只需要调整按钮大小。 enter image description here

import UIKit

@IBDesignable
class ButtonTableViewCell: UITableViewCell {

@IBInspectable var selectedColor : UIColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0)
@IBInspectable var normalColor : UIColor = UIColor.init(red: 59/255, green: 169/255, blue: 246/255, alpha: 1.0)


@IBOutlet weak var variant4button: UIButton!
@IBOutlet weak var variant3button: UIButton!
@IBOutlet weak var variant2Button: UIButton!
@IBOutlet weak var variant1button: UIButton!

这是4个按钮。 我在这里设置标题:

if (numberOfVariants.count != 0) {
            let questionTextView = cell.contentView.viewWithTag(5) as! UITextView
            questionTextView.text = "\(Questions[indexPath.row].content!)"
            variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside)
            variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside)
            variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside)
            variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside)
            let numberOfVars = numberOfVariants[indexPath.row]
            if (numberOfVars == 2) {
                variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
                variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
                variant3.isHidden = true
                variant4.isHidden = true
            }
            else if (numberOfVars == 3){
                variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
                variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
                variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
                variant4.isHidden = true
            }
            else if (numberOfVars == 4) {
                variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
                variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
                variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
                variant4.setTitle(Variants[3+amaunty[indexPath.row]].title, for: .normal)
            }
        }

为Jaydeep Vyas enter image description here

2 个答案:

答案 0 :(得分:1)

只需创建类uibutton并将此类分配给所需的按钮

class AutoSizableButton: UIButton
{
    override var intrinsicContentSize: CGSize
        {
        get {
            let labelSize = titleLabel?.sizeThatFits(CGSize(width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude)) ?? CGSize.zero
            let reqiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom)

            return reqiredButtonSize
        }
    }
}

也不要在viewDidload

中添加它
self.btnDemo.titleLabel?.numberOfLines = 0;
        self.btnDemo.titleLabel?.lineBreakMode = .byWordWrapping;

<强>输出 enter image description here

答案 1 :(得分:0)

以编程方式尝试:

- (float) getRequiredWidth : (UIButton *)button
{
    float requiredWidth =
    [button.titleLabel.text
     boundingRectWithSize:button.frame.size
     options:NSStringDrawingUsesLineFragmentOrigin
     attributes:@{ NSFontAttributeName:button.font }
     context:nil]
    .size.width;

    return requiredWidth;
}
你的代码中的

float totalWidth = [self getRequiredWidth : yourButton];
yourButton.frame = CGRectMake(yourButtonX, yourButtonY, totalWidth, yourButtonHeight)