[英]UIButton background doesn't cover whole view
I use uibutton to display some text in title ,sometimes text is long and I set 我使用uibutton在标题中显示一些文本,有时文本很长,我设置
text.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap;
when there are 2 or more lines , background doesn't have enough height 当有2行或更多行时,背景没有足够的高度
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);
To makes things more clear: 为了使事情更清楚:
I think your problem might be cause by your .SizeToFit() method. 我认为您的问题可能是由.SizeToFit()方法引起的。 Generally, you want the .SizeToFit() method to be the last modification of a UIButton, or any other control for that matter.
通常,您希望.SizeToFit()方法是UIButton的最后一次修改,或任何其他控件。 This is to prevent .Frame issues, and text not properly displaying.
这是为了防止.Frame问题,并且文本没有正确显示。 This probably goes for the background of the UIButton too.
这可能也适用于UIButton的背景。 So:
所以:
Let me know if it works. 如果有效,请告诉我。 Good luck!
祝好运!
Love and regards, Björn 爱和问候,Björn
Have you tried using a 9patch drawable as your background image? 您是否尝试过使用9patch drawable作为背景图片? 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
更多信息在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.