【问题标题】:How to set the autoSize to UIbutton according to Title of Button?如何根据按钮的标题将autoSize设置为UIbutton?
【发布时间】:2017-07-18 08:56:09
【问题描述】:

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

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

为杰迪普·维亚斯

【问题讨论】:

    标签: ios swift uibutton


    【解决方案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;
    

    输出

    【讨论】:

    • 它不工作,我添加了这段代码,但一切都是一样的。请结帐我已将照片添加到问题中
    • @АзаматБулегенов 它对我来说非常适合分配任何新的约束或忘记为按钮分配类
    • 我认为 TableViewCell 中的问题是我的单元格不允许更改此按钮的大小。可以给我你的邮箱吗?我发给你,也许你会更明白?
    【解决方案2】:

    以编程方式尝试:

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

    【讨论】:

    • 我对按钮的宽度没有任何限制,而且它不起作用。大小不变
    猜你喜欢
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多