iOS: UIButton resize according to text length

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

    If your button was made with Interface Builder, and you're changing the title in code, you can do this:

    [self.button setTitle:@"Button Title" forState:UIControlStateNormal];
    [self.button sizeToFit];
    
    0 讨论(0)
  • 2020-11-29 18:27

    This button class with height autoresizing for text (for Xamarin but can be rewritten for other language)

    [Register(nameof(ResizableButton))]
    public class ResizableButton : UIButton
    {
        NSLayoutConstraint _heightConstraint;
        public bool NeedsUpdateHeightConstraint { get; private set; } = true;
    
        public ResizableButton(){}
    
        public ResizableButton(UIButtonType type) : base(type){}
    
        public ResizableButton(NSCoder coder) : base(coder){}
    
        public ResizableButton(CGRect frame) : base(frame){}
    
        protected ResizableButton(NSObjectFlag t) : base(t){}
    
        protected internal ResizableButton(IntPtr handle) : base(handle){}
    
        public override void LayoutSubviews()
        {
            base.LayoutSubviews();
            UpdateHeightConstraint();
            InvalidateIntrinsicContentSize();
        }
    
        public override void SetTitle(string title, UIControlState forState)
        {
            NeedsUpdateHeightConstraint = true;
            base.SetTitle(title, forState);
        }
    
        private void UpdateHeightConstraint()
        {
            if (!NeedsUpdateHeightConstraint)
                return;
            NeedsUpdateHeightConstraint = false;
            var labelSize = TitleLabel.SizeThatFits(new CGSize(Frame.Width - TitleEdgeInsets.Left - TitleEdgeInsets.Right, float.MaxValue));
    
            var rect = new CGRect(Frame.X, Frame.Y, Frame.Width, labelSize.Height + TitleEdgeInsets.Top + TitleEdgeInsets.Bottom);
    
            if (_heightConstraint != null)
                RemoveConstraint(_heightConstraint);
    
            _heightConstraint = NSLayoutConstraint.Create(this, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1, rect.Height);
            AddConstraint(_heightConstraint);
        }
    
         public override CGSize IntrinsicContentSize
         {
             get
             {
                 var labelSize = TitleLabel.SizeThatFits(new CGSize(Frame.Width - TitleEdgeInsets.Left - TitleEdgeInsets.Right, float.MaxValue));
                 return new CGSize(Frame.Width, labelSize.Height + TitleEdgeInsets.Top + TitleEdgeInsets.Bottom);
             }
         }
    }
    
    0 讨论(0)
提交回复
热议问题