iOS: UIButton resize according to text length

后端 未结 14 627
你的背包
你的背包 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:00

    For some reason, func sizeToFit() does not work for me. My set up is I am using a button inside a UITableViewCell and I am using auto layout.

    What worked for me is:

    1. get the width constraint
    2. get the intrinsicContentSize width because according the this document auto layout is not aware of the intrinsicContentSize. Set the width constraint to the intrinsicContentSize width

    Here're two titles from the Debug View Hierachry

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

    To accomplish this using autolayout, try setting a variable width constraint:

    You may also need to adjust your Content Hugging Priority and Content Compression Resistance Priority to get the results you need.


    UILabel is completely automatically self-sizing:

    This UILabel is simply set to be centered on the screen (two constraints only, horizontal/vertical):

    It changes widths totally automatically:

    You do not need to set any width or height - it's totally automatic.

    Notice the small yellow squares are simply attached ("spacing" of zero). They automatically move as the UILabel resizes.

    Adding a ">=" constraint sets a minimum width for the UILabel:

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

    To be honest I think that it's really shame that there is no simple checkbox in storyboard to say that you want to resize buttons to accommodate the text. Well... whatever.

    Here is the simplest solution using storyboard.

    1. Place UIView and put constraints for it. Example:
    2. Place UILabel inside UIView. Set constraints to attach it to edges of UIView.

    3. Place your UIButton inside UIView. Set the same constraints to attach it to the edges of UIView.

    4. Set 0 for UILabel's number of lines.

    5. Set up the outlets.

        @IBOutlet var button: UIButton!
        @IBOutlet var textOnTheButton: UILabel!
    
    1. Get some long, long, long title.
    
        let someTitle = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    
    1. On viewDidLoad set the title both for UILabel and UIButton.
    
        override func viewDidLoad() {
            super.viewDidLoad()
            textOnTheButton.text = someTitle
            button.setTitle(someTitle, for: .normal)
            button.titleLabel?.numberOfLines = 0
        }
    
    1. Run it to make sure that button is resized and can be pressed.

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

    Swift:

    The important thing here is when will you call the .sizeToFit() , it should be after setting the text to display so it can read the size needed, if you later update text, then call it again.

    var button = UIButton()
    button.setTitle("Length of this text determines size", forState: UIControlState.Normal)
    button.sizeToFit()
    self.view.addSubview(button)
    
    0 讨论(0)
  • 2020-11-29 18:05

    I have some additional needs related to this post that sizeToFit does not solve. I needed to keep the button centered in it's original location, regardless of the text. I also never want the button to be larger than its original size.

    So, I layout the button for its maximum size, save that size in the Controller and use the following method to size the button when the text changes:

    + (void) sizeButtonToText:(UIButton *)button availableSize:(CGSize)availableSize padding:(UIEdgeInsets)padding {    
    
         CGRect boundsForText = button.frame;
    
        // Measures string
        CGSize stringSize = [button.titleLabel.text sizeWithFont:button.titleLabel.font];
        stringSize.width = MIN(stringSize.width + padding.left + padding.right, availableSize.width);
    
        // Center's location of button
        boundsForText.origin.x += (boundsForText.size.width - stringSize.width) / 2;
        boundsForText.size.width = stringSize.width;
        [button setFrame:boundsForText];
     }
    
    0 讨论(0)
  • 2020-11-29 18:09

    The way to do this in code is:

    [button sizeToFit];
    

    If you are subclassing and want to add extra rules you can override:

    - (CGSize)sizeThatFits:(CGSize)size;
    
    0 讨论(0)
提交回复
热议问题