【问题标题】:How set UIButton size by it's title size?如何通过标题大小设置 UIButton 大小?
【发布时间】:2018-02-16 12:23:35
【问题描述】:

我有 UIButton,它的标题是动态变化的。按钮大小应随标题大小而变化,并且与标题大小相同。

如何在 Swift 中以编程方式执行此操作?

【问题讨论】:

    标签: swift uibutton cgsize


    【解决方案1】:

    要让您的按钮使用其固有内容大小并根据其文本自动调整大小,请使用自动布局来定位按钮。只需设置约束来定位按钮,iOS 将使用文本的大小来确定按钮的宽度和高度。

    例如:

    let button = UIButton()
    
    // tell it to NOT use the frame
    button.translatesAutoresizingMaskIntoConstraints = false
    
    button.setTitle("Hello", for: .normal)
    view.addSubview(button)
    
    button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    

    如果您在情节提要中创建按钮,这也适用。同样,只给按钮放置约束,它会调整大小以适应文本。

    【讨论】:

      【解决方案2】:

      你可以通过title动态获取UIButton'swidthHeight

      借助 NSString 的 Size 属性我们可以做到这一点。

      let buttonNAme = [" hi ", "welcome", "Login", "Forgot Password ??", "New to here. Sign up??"]
      var yPos = CGFloat()
      
      override func viewWillAppear(_ animated: Bool) {
      
          yPos = 40
      
          for i in 0..<buttonNAme.count
          {
              self.view.addSubview(addingCustomButton(buttonTitle: buttonNAme[i], buttonFontSize: 15, buttonCount: i))
          }
      }
      
      func addingCustomButton(buttonTitle : String, buttonFontSize: CGFloat, buttonCount : Int) -> UIButton
      {
          let ownButton = UIButton()
      
          ownButton.setTitle(buttonTitle, for: UIControlState.normal)
      
      
          ownButton.titleLabel?.font = UIFont.systemFont(ofSize: buttonFontSize)
      
          let buttonTitleSize = (buttonTitle as NSString).size(attributes: [NSFontAttributeName : UIFont.boldSystemFont(ofSize: buttonFontSize + 1)])
      
          ownButton.frame.size.height = buttonTitleSize.height * 2
          ownButton.frame.size.width = buttonTitleSize.width
          ownButton.frame.origin.x = 30
      
          yPos = yPos + (ownButton.frame.size.height) + 10
      
          ownButton.frame.origin.y = yPos 
      
          ownButton.tintColor = UIColor.white
          ownButton.backgroundColor = .brown
      
          ownButton.tag = buttonCount
      
          ownButton.setTitleColor(UIColor.darkGray, for: UIControlState.highlighted)
          ownButton.addTarget(self, action: #selector(ownButtonAction), for: UIControlEvents.touchUpInside)
      
          return ownButton
      }
      
      func ownButtonAction(sender: UIButton)
      {
          print("\n\n Title  \(sender.titleLabel?.text)  TagNum    \(sender.tag)")
      }
      

      输出

      【讨论】:

        【解决方案3】:

        按照以下步骤操作(这不是一个正确的解决方案,但您可以通过这样做来解决您的问题)

        1. 创建一个UILabel(因为UILabel调整它的高度和宽度取决于文本)
        2. UIlabel 行号为 1
        3. 在 UILabel 上创建一个 UIButton
        4. 将按钮标题设置为“”
        5. 设置按钮的约束:对齐按钮的顶部并通向 UILabel 并等于宽度和高度

        希望这对你有用:)

        【讨论】:

        • 没关系,但是如果我想在按下按钮时获得文本的按下效果怎么办?当我设置按钮标题并按下但我有这个效果
        猜你喜欢
        • 2015-02-08
        • 2021-01-13
        • 1970-01-01
        • 2018-12-11
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 2016-06-04
        • 1970-01-01
        相关资源
        最近更新 更多