Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何根据按钮标题将autoSize设置为UIbutton?_Ios_Swift_Uibutton - Fatal编程技术网

Ios 如何根据按钮标题将autoSize设置为UIbutton?

Ios 如何根据按钮标题将autoSize设置为UIbutton?,ios,swift,uibutton,Ios,Swift,Uibutton,我有纽扣。为了获取按钮的标题,我向服务器发送请求,然后设置按钮的标题。现在我需要将自动调整大小设置为该按钮。如何做到这一点?我在按钮的标识检查器中设置为换行字符换行。现在文本显示在多行中,但我还需要调整按钮的大小,因为有些文本太大,有些太小。这里是它在故事板和约束中的样子,所有的都好,我只需要调整按钮的大小。 这是4个按钮。 我在这里设置标题: if (numberOfVariants.count != 0) { let questionTextView = cell.

我有纽扣。为了获取按钮的标题,我向服务器发送请求,然后设置按钮的标题。现在我需要将自动调整大小设置为该按钮。如何做到这一点?我在按钮的标识检查器中设置为换行字符换行。现在文本显示在多行中,但我还需要调整按钮的大小,因为有些文本太大,有些太小。这里是它在故事板和约束中的样子,所有的都好,我只需要调整按钮的大小。

这是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

以编程方式尝试以下操作:

- (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)

只需创建类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;
输出

我对按钮的宽度没有任何限制,它不起作用。大小不改变它不工作,我添加了这个代码,但一切都是一样的。请检查我已将照片添加到问题@АззззззззззззззззБззззз。你能给我你的电子邮件吗?我会寄给你,也许你会更明白?登录谢尔盖密码-123456Let我们。
self.btnDemo.titleLabel?.numberOfLines = 0;
        self.btnDemo.titleLabel?.lineBreakMode = .byWordWrapping;