UILabel默认是垂直居中的,高度是多少就对折居中,而UITextField却不是这样,它是置顶的(当然,有个默认的类似css的padding,这里不说这个),那怎么让它按高度折中居中呢,VerticalAlignment属性即可
UITextField utxt = new UITextField (recf);
utxt.VerticalAlignment = UIControlContentVerticalAlignment.Center;
这样就居中了,具体自己看效果,水平居中是UITextAlignment属性:
utxt.TextAlignment = UITextAlignment.Center;
配合枚举,可以动态设置的,比如:
utxt.TextAlignment = (UITextAlignment)Enum.Parse (typeof(UITextAlignment), attr [9]);
设置文字大小和类型:
utxt.Font = UIFont.FromName (Utils.Tools.GetFontStyle (attr [3]), f (attr [4]));
public static string GetFontStyle(string f)
{
return f.Replace ("_", "-");
}
Font属性即可,我这里做了动态配置,所以需要转一道。另外说一下,ios中的字体文本特点(我说的是文本特点)和css中一样,以-符号间隔(不是下划线_),比如:
public enum FontStyle
{
AcademyEngravedLetPlain,
AmericanTypewriter_CondensedLight,
AmericanTypewriter_Light,
AmericanTypewriter,
AmericanTypewriter_Condensed,
。。。
上面说font的文本是题外话。
下面说UILabel,对它最常见的设置是自动换行,需要做如下设置:
public static UILabel CreateLbl( RectangleF frame, string title, int line=0 ,bool clearbgc=true)
{
UILabel l = new UILabel (frame);
l.Text = title;
l.AdjustsFontSizeToFitWidth = true;
l.LineBreakMode = UILineBreakMode.TailTruncation;
l.Lines = line;
if (clearbgc)
l.BackgroundColor = UIColor.Clear;
else
l.BackgroundColor = UIColor.Red; //测试布局用的
return l;
}
最主要的是
LineBreakMode属性,lines属性指定uilabel区域高度可划分几行排列文字,默认是1。这里顺便说一下,xamarin开发环境下的编程风格有点像java,除最外层的打括号是正对排列的,其它都是斜角排列,类似javascript,所以要习惯它。
对于颜色设置不多说了,想说一下UIColor.FromRGB 方法,从字面上很好理解,就是通过rgb3个数值来得到颜色,比如:
public static void SetFontColor( UILabel c,string strRGB)
{
string[] rgb = strRGB.Split (',');
c.TextColor = UIColor.FromRGB (FormateObject.GetInt (rgb [0]),
FormateObject.GetInt (rgb [1]),
FormateObject.GetInt (rgb [2]));
}
后续在补充吧