在一個應用程序中,文字是很是重要的。它就是這些不會說話的設備的嘴巴。經過這些文字,能夠很清楚的指定這些應用程序要表達的信息。如下將爲開發者介紹3種關於文本的視圖。ide
標籤視圖(通常使用UILabel類實現)通常用於在應用程序中爲用戶顯示少許的信息。工具
【示例2-13】如下就是經過標籤視圖爲開發者顯示一首詩的效果。具體步驟以下:this
(1)建立一個Single View Application類型的工程,命名爲2-20。spa
(2)添加圖像1.jpg到建立工程的Resources文件夾中。orm
(3)打開MainStoryboard.storyboard文件,對主視圖進行設置。效果如圖2.26所示。教程
圖2.26 主視圖的效果開發
須要添加的視圖以及設置如表2-6所示。it
表2-6 設置主視圖io
(4)打開2-20ViewController.cs文件,編寫代碼,實現爲主視圖添加標籤的功能。代碼以下:form
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __20ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UILabel label1 = new UILabel ();
label1.Frame = new RectangleF (2, 410, 155, 28);
label1.TextAlignment = UITextAlignment.Center; //設置標籤文本內容的對其方式
label1.Text = "碧玉妝成一樹高,"; //設置標籤的文本內容
this.View.AddSubview (label1);
……
UILabel label4 = new UILabel ();
label4.Frame = new RectangleF (2, 500, 155, 28);
label4.TextAlignment = UITextAlignment.Center;
label4.Text = "二月春風似剪刀.";
this.View.AddSubview (label4);
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.27所示。
圖2.27 運行效果
注意:在此程序中,使用了TextAlignment屬性設置了文本在標籤中的對齊方式。使用Text屬性設置了標籤中顯示的文本。標籤視圖默認是顯示一行的,可是,也能夠將標籤的內容顯示爲多行。
【示例2-14】如下將在一個標籤中顯示3行文本內容。具體步驟以下:
(1)建立一個Single View Application類型的工程,命名爲2-23。
(2)添加圖像1.jpg到建立工程的Resources文件夾中。
(3)打開MainStoryboard.storyboard文件,從工具欄中拖動Image View圖像視圖到主視圖中,將此視圖的Image屬性設置爲1.jpg。
(4)打開2-23ViewController.cs文件,編寫代碼,實現標籤多行顯示的功能。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __23ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UILabel label = new UILabel ();
label.Frame = new RectangleF (20, 100, 280, 64);
label.Text = " 如何讓你碰見我,在我最美麗的時刻。爲這,我已在佛前求了五百年,求他讓咱們結一段塵緣。";
label.Lines = 3; //設置顯示文本的行數
this.View.AddSubview (label);
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.28所示。
圖2.28 運行效果
當標籤中的內容過多時,開發者能夠對標籤中顯示的內容進行省略,便可以設置內容顯示的格式。在Xamarin中有6種風格,如表2-7所示。
表2-7 內容顯示的格式
對於內容顯示格式的設置須要使用到LineBreakMode屬性。
【示例2-15】下面將使用LineBreakMode屬性讓在標籤中顯示的內容截去中間部分。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __33ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UILabel label = new UILabel ();
label.Frame = new RectangleF (20, 100, 280, 64);
label.Text = " 如何讓你碰見我,在我最美麗的時刻。爲這,我已在佛前求了五百年,求他讓咱們結一段塵緣。";
label.LineBreakMode = UILineBreakMode.MiddleTruncation; //設置內容顯示的格式
this.View.AddSubview (label);
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
此時運行程序,會看到如圖2.29所示的效果。
圖2.29 運行效果