想学用C#开发iphone 的就加入 QQ群:178290571 ,让我们共同进步吧!
C# MonoTouch for iphone 开发blog http://blog.csdn.net/ssihc0/
MonoDevelop 版本:2.8.0
MonoTouch 版本:4.2.2
UILabel 是一个只读文本视图,和windows 下的TextBox差不多,通过设置属性可以控制标签中文本中的字体,颜色,对齐方式,突出显示和阴影。
下面是方法和属性:
主要属性 :
Text 标签上实际显示的文本。
textALignment 使用UITextAlignment 枚举来选择Left,Center 或Right 对齐方式。
Font 设置 文本的字体。
TextColor 指定显示文字的颜色
Lines 显示的最大行数。
新建一个工程名为test 打开testViewController
添加下面代码
public partial class testViewController : UIViewController
{
private UILabel label1;
public testViewController (string nibName, NSBundle bundle) : base (nibName, bundle)
{
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var label= new UILabel();
label.Font= UIFont.FromName("Times New Roman",10f);
label.Text="Hello World";
label.TextColor=UIColor.Red;
label.Lines=3;
label.Frame=new System.Drawing.RectangleF(35,30,250,40);
this.View.AddSubview(label);
label1= new UILabel(new System.Drawing.RectangleF(35,130,250,40));
label1.Text="Hello World !!!";
label1.BaselineAdjustment=UIBaselineAdjustment.AlignCenters;
label1.TextAlignment=UITextAlignment.Center;
label1.ShadowColor=UIColor.LightTextColor;
this.View.AddSubview(label1);
//any additional setup after loading the view, typically from a nib.
}
public override void ViewDidUnload ()
{
base.ViewDidUnload ();
// Release any retained subviews of the main view.
// e.g. myOutlet = null;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
}
}
下面分析代码:
private UILabel label1; 申明 UILabel
public override void ViewDidLoad () 当视图加载的时候触发事件
base.ViewDidLoad ();调用基类的ViewDidLoad
var label= new UILabel(); 这是另一种建对象的方式,,上面我们使用了private UILabel label1; label.Font= UIFont.FromName("Times New Roman",10f); 设置字体 label.Text="Hello World"; 设置要显示的文本 label.TextColor=UIColor.Red; 设置color label.Lines=3; 设置最大显示行 label.Frame=new System.Drawing.RectangleF(35,30,250,40); 设置显示的位置,和区域 this.View.AddSubview(label); 把label 显示到View 上
label1= new UILabel(new System.Drawing.RectangleF(35,130,250,40)); 另一种实列化方式,把 设置显示的位置,和区域 当实例化信息
label1.Text="Hello World !!!";
label1.TextAlignment=UITextAlignment.Center; 设置中间对齐
label1.ShadowColor=UIColor.LightTextColor;
this.View.AddSubview(label1) 添加到view 里,
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
}你也可以根据toInterfaceOrientation的不同值来判断是否允许旋转。这个传入参数有四种取值:
public enum UIInterfaceOrientation
{
Portrait = 1, 正常
PortraitUpsideDown, 反向Home键在上
LandscapeLeft = 4, 横向Home键在左
LandscapeRight = 3 横向Home键在右
}运行结果
源代码:
下载后把gif 改成zip