0

I use uibutton to display some text in title ,sometimes text is long and I set

text.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap; 

when there are 2 or more lines , background doesn't have enough height enter image description here

This is my code for button:

text = new UIButton ();
        text.Layer.CornerRadius = 5;
        text.Font = UIFont.FromName ("DIN Condensed", text.Font.PointSize);

        text.SetTitle (Original._hint, UIControlState.Normal);
        text.SizeToFit ();
        text.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap; 

        var rec = new UILongPressGestureRecognizer (() => {     

            text.Frame = new CGRect ((UIApplication.SharedApplication.KeyWindow.Frame.Width / 2.0f) - (250 / 2.0f),
                this.Superview.Superview.Superview.Frame.Height / 2.0f,250, text.Frame.Height);
            Animate (0.6f, () => {
                this.Superview.Superview.Superview.AddSubview (text);
            }, () => {
                text.Layer.ZPosition = int.MaxValue;
            });

            viewToremove=text;
        });


        text.SetTitleColor (UIColor.White, UIControlState.Normal);
        text.BackgroundColor = UIColor.Black;

        TargetView.AddGestureRecognizer (rec);
Nininea
  • 2,671
  • 6
  • 31
  • 57
  • I am not sure but have you checked that font is not cause for this. Check it by changing font to system font. – Kampai Feb 18 '15 at 08:04
  • yes, font doesn't cause problem :( – Nininea Feb 18 '15 at 08:11
  • Make your image stretchable, and I would not suggest you to add a gesture recognizer to a button, compose you own UIControl subclass or your own UIView element. Use inset to make enough room for your text – Andrea Feb 18 '15 at 08:23
  • Does it make any difference to swap around the two lines of code "text.SizeToFit ()" and " text.TitleLabel.LineBreakMode = ..." ? You're currently asking to resize the Button and THEN telling it that, actually, you want to use WordWrap... – Mike Gledhill Feb 18 '15 at 08:32
  • nope. I tried this , but it doesn't make seance @MikeGledhill – Nininea Feb 18 '15 at 08:34
  • Does your iPhone/iPad screen have AutoLayout turned on ? If so, do you have a height constraint on this control ? – Mike Gledhill Feb 18 '15 at 08:37
  • Or perhaps you need to modify the value of "text.translatesAutoresizingMaskIntoConstraints". – Mike Gledhill Feb 18 '15 at 08:39
  • @MikeGledhill I have tried , but it doesn't make seance :( – Nininea Feb 18 '15 at 08:43

3 Answers3

0

To makes things more clear:

  1. Make your image strechable, that can be easily done in the image asset directory by showing slicing here, this will guarantee that you button will keep the same aspect also in the corners
  2. Use button inset, you can do it in IB Inset Than I would avoid to use add a gesture recognizer to a UIButton, but I think it will work anyway.
Andrea
  • 26,120
  • 10
  • 85
  • 131
  • sorry, it's not clear what image are you talking about? – Nininea Feb 18 '15 at 08:39
  • the image that you are using as a background – Andrea Feb 18 '15 at 08:46
  • it is just a color, not image: text.BackgroundColor = UIColor.Black; – Nininea Feb 18 '15 at 08:48
  • I see, are you using autolayout? maybe you set a height constraint than force the button to be smaller that its intrinsic content size – Andrea Feb 18 '15 at 08:53
  • nope, StackLayout. I modified text.translatesAutoresizingMaskIntoConstraints property (as @MikeGledhill suggested to me ), but it doesn't make seance – Nininea Feb 18 '15 at 08:57
  • Don't really know, I don't develop in xamarin but in ObjC, just one last suggestion try to remove SizeToFit. The button should adapt itself based on its content. – Andrea Feb 18 '15 at 09:18
  • thanks anyway, but it is not good solution to remove SizeToFit, in this case there background color doesn't exist – Nininea Feb 18 '15 at 09:25
0

I think your problem might be cause by your .SizeToFit() method. Generally, you want the .SizeToFit() method to be the last modification of a UIButton, or any other control for that matter. This is to prevent .Frame issues, and text not properly displaying. This probably goes for the background of the UIButton too. So:

  • Initialise your button (I usually keep the width static (screenwidth) and set the height to a high number, like 200 or something).
  • Modify its properties (including .Title and .BackgroundColor)
  • Add a delegate or eventhandler.
  • Shrink the button using .SizeToFit();

Let me know if it works. Good luck!

Love and regards, Björn

Magicbjørn
  • 619
  • 9
  • 23
0

Have you tried using a 9patch drawable as your background image? Those are designed specifically for situations where you are unsure about the length of the content and require your background image to stretch/shrink to fit. More info here

Yus
  • 48
  • 3