【问题标题】:UIButton label text is being clippedUIButton 标签文本被剪裁
【发布时间】:2011-06-02 08:34:56
【问题描述】:

我在 Interface Builder 中内置了一个具有默认标签的 UIButton。在 Xcode 中,我正在动态更改标签文本,如下所示:

myButton.titleLabel.text = @"this is the new label";

但是,当文本更新时,新字符串被剪裁到与原始字符串相同的大小,最终看起来像:

this...label

有人知道为什么会这样吗?

【问题讨论】:

    标签: iphone cocoa-touch uibutton


    【解决方案1】:

    在您的按钮上拨打sizeToFit。这将调整按钮的大小以适合文本。

    【讨论】:

    • setNeedsLayout 没有帮助我,但 sizeToFit 解决了这个问题。
    【解决方案2】:

    您应该使用setTitle:forState: 来更改UIButton 的标题。如果您自己更改标题,则按钮不会显示需要调整标签大小 - 您最终将不得不执行以下操作:

    myButton.titleLabel.text = @"this is the new label";
    [myButton setNeedsLayout];
    

    但我什至不确定这是否适用于所有情况。提供了setTitle:forState: 之类的方法,以便您可以为多个状态提供标题,而无需手动更新按钮,并且按钮知道需要使用新标题对其进行布局。

    【讨论】:

    • 当然。获取动态按钮文本以调整按钮 titleLabel 大小的正确方法是通过 setTitle:forState:
    • 或者只是,btnChooseAnAccount.setTitle(displayName, for: .normal)
    【解决方案3】:

    如果这不起作用,您可以随时确定字符串大小并调整按钮框架宽度。在这种情况下,您确定它会合适。

    // Calculate the size 
    CGSize buttonSize = [@"My text.." sizeWithFont:[UIFont systemFontOfSize:15.0]
                        constrainedToSize:someSize lineBreakMode:UILineBreakModeWordWrap];
    
    // Do whatever you want with the "buttonSize", you can for example adjust your button's frame width
    

    【讨论】:

      【解决方案4】:

      另一种解决方案是让 UIButton 的内部 UILabel 缩小字体大小,就像 UILabel 可以做的那样:

      button.titlelabel.minimumFontSize = 8.0; // or some more adequate size
      self.buttonWithLongTitle.titleLabel.adjustsFontSizeToFitWidth = YES;
      

      【讨论】:

      • 这是唯一对我有用的东西......按钮没有被拉伸,但至少你可以看到文字
      • 注意:minimumFontSize 现已弃用。请改用minimumScaleFactor。 IE。完全停止缩小,使用minimumScaleFactor = 1.0;
      • 这是正确的答案,特别是 adjustsFontSizeToFitWidth 是 OP 正在寻找的方法。
      • 我认为这不是正确的答案。在大多数情况下,满足于任意字体大小是不可接受的妥协。
      • 简直太棒了;)
      【解决方案5】:

      尝试使用按钮的 setTitle 方法(而不是直接在标签上设置标题)。它应该强制调整标题标签的大小。

      目标 C:

      [myButton setTitle:@"This is the text" forState:UIControlStateNormal];
      

      或者在 Swift 中:

      myButton.setTitle("This is the text", for: .normal)
      

      【讨论】:

      • +1 以获得完美答案。我尝试了上面给出的所有两种选择。但除了这个答案之外,无法解决我的问题。
      • 而不是选择的最佳答案,这对我来说非常适合! +1
      • 这是最直接最正确的。回答。
      【解决方案6】:

      Swift 4.2 中的解决方案

      yourButton.titleLabel?.minimumScaleFactor = 0.5 //set whatever you want here to scale
      yourButton.titleLabel?.adjustsFontSizeToFitWidth = true
      

      Objective C的解决方案

      [yourButton.titleLabel setMinimumScaleFactor:0.5];
      [yourButton.titleLabel setAdjustsFontSizeToFitWidth:YES];
      

      【讨论】:

        【解决方案7】:

        这对我有用,同时通过所有其他约束以编程方式设置我的按钮。

               yourButton.widthAnchor.constraint(equalToConstant: yourButton.intrinsicContentSize.width).isActive = true
        

        你也可以像这样添加“填充”

               yourButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
        

        【讨论】:

          【解决方案8】:

          如果未选中,请使用Autoresizing mask

          【讨论】:

            猜你喜欢
            • 2014-09-07
            • 2012-04-29
            • 1970-01-01
            • 1970-01-01
            • 2022-01-21
            • 1970-01-01
            • 2015-08-15
            • 2018-03-13
            • 2017-03-07
            相关资源
            最近更新 更多