最近在使用SunnyUI的控件写东西,遇到非常难受的问题,Button不支持换行,下载了Sunny UI源码,修改内容如下
找到UIButton代码新增重写
private string _text; // 重写 Text 属性 [Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] public override string Text { get { return _text; } set { if (_text != value) { _text = value; Invalidate(); } } }
找到重绘文件修改如下
public static void DrawString(this Graphics g, string str, Font font, Color color, Size size, Padding padding, ContentAlignment align) { if (str.IsNullOrEmpty()) return; SizeF sf = g.MeasureString(str, font); using (Brush br = color.Brush()) { StringFormat stringFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; stringFormat.FormatFlags = StringFormatFlags.LineLimit; switch (align) { case ContentAlignment.MiddleCenter: //g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, // padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f,stringFormat); RectangleF rect = new RectangleF(0, 0, size.Width, size.Height); g.DrawString(str, font, br, rect, stringFormat); break; case ContentAlignment.TopLeft: g.DrawString(str, font, br, padding.Left, padding.Top); break; case ContentAlignment.TopCenter: g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, padding.Top); break; case ContentAlignment.TopRight: g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, padding.Top); break; case ContentAlignment.MiddleLeft: g.DrawString(str, font, br, padding.Left, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); break; case ContentAlignment.MiddleRight: g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f); break; case ContentAlignment.BottomLeft: g.DrawString(str, font, br, padding.Left, size.Height - sf.Height - padding.Bottom); break; case ContentAlignment.BottomCenter: g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f, size.Height - sf.Height - padding.Bottom); break; case ContentAlignment.BottomRight: g.DrawString(str, font, br, size.Width - sf.Width - padding.Right, size.Height - sf.Height - padding.Bottom); break; } } }
实在找不到就全工程搜索,
标签:SunnyUI,换行,Button,Height,padding,Width,str,sf,size From: https://www.cnblogs.com/wxt51/p/17353293.html