在一个应用程序中,文字是很是重要的。它就是这些不会说话的设备的嘴巴。经过这些文字,能够很清楚的指定这些应用程序要表达的信息。如下将为开发者介绍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 运行效果