iOS: UIButton resize according to text length

后端 未结 14 628
你的背包
你的背包 2020-11-29 17:28

In interface builder, holding Command + = will resize a button to fit its text. I was wondering if this was possible to do programmatically before the

相关标签:
14条回答
  • 2020-11-29 18:09

    Simply:

    1. Create UIView as wrapper with auto layout to views around.
    2. Put UILabel inside that wrapper. Add constraints that will stick tyour label to edges of wrapper.
    3. Put UIButton inside your wrapper, then simple add the same constraints as you did for UILabel.
    4. Enjoy your autosized button along with text.
    0 讨论(0)
  • 2020-11-29 18:14

    In Xcode 4.5 and above, this can now be done by using 'Auto-layouting / Constraints'.

    Major advantages are that:

    1. You don't need to programmatically set frames at all!
    2. If done right, you don't need to bother about resetting frames for orientation changes.
    3. Also, device changes needn't bother you (read, no need to code separately for different screen sizes).

    A few disadvantages:

    1. Not backward compatible - works only for iOS 6 and above.
    2. Need to get familiarised (but will save time later on).

    Coolest thing is we get to focus on declaring an intent such as:

    • I want these two buttons to be of the same width; or
    • I need this view to be vertically centered and extend to a max entent of 10 pts from the superview's edge; or even,
    • I want this button/label to resize according to the label it is displaying!

    Here is a simple tutorial to get introduced to auto-layouting.

    For a more details.

    It takes some time at first, but it sure looks like it will be well worth the effort.

    0 讨论(0)
  • 2020-11-29 18:15

    If you want to resize the text as opposed to the button, you can use ...

    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    button.titleLabel.minimumScaleFactor = .5;
    // The .5 value means that I will let it go down to half the original font size 
    // before the texts gets truncated
    
    // note, if using anything pre ios6, you would use I think
    button.titleLabel.minimumFontSize = 8;
    
    0 讨论(0)
  • 2020-11-29 18:17

    In UIKit, there are additions to the NSString class to get from a given NSString object the size it'll take up when rendered in a certain font.

    Docs was here. Now it's here under Deprecated.

    In short, if you go:

    CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:14]]; 
    //or whatever font you're using
    [button setFrame:CGRectMake(10,0,stringsize.width, stringsize.height)];
    

    ...you'll have set the button's frame to the height and width of the string you're rendering.

    You'll probably want to experiment with some buffer space around that CGSize, but you'll be starting in the right place.

    0 讨论(0)
  • 2020-11-29 18:22

    sizeToFit doesn't work correctly. instead:

    myButton.size = myButton.sizeThatFits(CGSize.zero)
    

    you also can add contentInset to the button:

    myButton.contentEdgeInsets = UIEdgeInsetsMake(8, 8, 4, 8)

    0 讨论(0)
  • 2020-11-29 18:24

    Swift 4.2

    Thank god, this solved. After setting a text to the button, you can retrieve intrinsicContentSize which is the natural size from an UIView (the official document is here). For UIButton, you can use it like below.

    button.intrinsicContentSize.width
    

    For your information, I adjusted the width to make it look properly.

    button.frame = CGRect(fx: xOffset, y: 0.0, width: button.intrinsicContentSize.width + 18, height: 40)
    

    Simulator

    UIButtons with intrinsicContentSize

    Source: https://riptutorial.com/ios/example/16418/get-uibutton-s-size-strictly-based-on-its-text-and-font

    0 讨论(0)
提交回复
热议问题