UIButton with subtitle text

tomschuringtomschuring AUMember ✭✭
edited October 10 in Xamarin.iOS

How would i go about adding a subtitle (in smaller font) underneath the Title on a UIButton ?

do i need to subclass a UIButton ? or is there another solution ?

Tagged:

Best Answer

Answers

  • tomschuringtomschuring AUMember ✭✭
    edited October 10

    @DannyC thank you !

    i did do a few google searches before asking here, i think it was because i kept adding the word Xamarin to the query.

    thank you !!

  • tomschuringtomschuring AUMember ✭✭

    just for anyone else looking for something like this:

    [Register("ButtonSubTitle")]
        public class ButtonSubTitle : UIButton
        {
            UILabel _SubTitle;
    
            public ButtonSubTitle(string text)
                : base(NSObjectFlag.Empty)
            {
                SetTitle(text, UIControlState.Normal);
                AddSubTitle();
            }
            public ButtonSubTitle(string text, EventHandler handler)
            {
                TouchUpInside += handler;
                SetTitle(text, UIControlState.Normal);
                AddSubTitle();
            }
            public ButtonSubTitle(IntPtr handle) : base(handle)
            {
                AddSubTitle();
            }
    
            public override void LayoutSubviews()
            {
                base.LayoutSubviews();
                if(_SubTitle != null)
                {
                    CGRect frame = _SubTitle.Frame;
                    frame.Width = Frame.Width;
                    frame.Height = 30;
                    frame.X = 0;
    
                    frame.Y = Frame.Height - frame.Height - 10;
                    _SubTitle.Frame = frame;
                }
            }
    
            private void AddSubTitle()
            {
                _SubTitle = new UILabel();
                _SubTitle.Text = "bla bla bla";
                _SubTitle.TextColor = MvxToUiColor(new MvxColor(0xFE, 0xFE, 0xFE));
                _SubTitle.TextAlignment = UITextAlignment.Center;
                this.AddSubview(_SubTitle);
            }
    
    
            [Export("setSubTitle:forState:")]
            public void SetSubTitle(string subTitle, UIControlState forState)
            {
                this._SubTitle.Text = subTitle;
            }
    
            public string SubTitle
            {
                get { return this._SubTitle.Text; }
                set { this._SubTitle.Text = value; }
            }
    
            private UIColor MvxToUiColor(MvxColor mvxColor)
            {
                return new UIColor(mvxColor.R / 255F, mvxColor.G / 255F, mvxColor.B / 255F, mvxColor.A / 255F);
            }
        }
    
  • DannyCDannyC US ✭✭✭✭

    @tomschuring glad you worked it out and thank you for posting your subclass example.

Sign In or Register to comment.