Displaying Text Content in iOS
The text system in iOS provides a tremendous amount of power while still being very simple to use. The UIKit framework includes several high-level classes for managing the display and input of text. UIKit also includes a class for displaying HTML, CSS, and JavaScript-based web content.
iOS中的文本系统提供了大量简单易用的功能。 UIKit 框架包括几个用于管理文本显示和输入的高级类。 UIKit还包括一个用于显示HTML,CSS和基于JavaScript的Web内容的类。
Use Text Objects to Display Text Content(使用Text对象布局文本)
Text objects display styled, formatted text in a range of fonts, styles, and sizes. The UIKit framework provides three primary classes for displaying this text content in an app’s user interface:
- UILabel defines a label, which displays a static text string.
- UITextField defines a text field, which displays a single line of editable text.
- UITextView defines a text view, which displays multiple lines of editable text.
文本对象用于显示具有字体、样式、大小的格式化文本。 UIKit框架提供了三个主要类,用于在应用程序的用户界面中显示此文本内容:
- UILabel定义了一个标签,显示一个静态文本字符串。
- UITextField定义一个文本字段,显示一行可编辑的文本。
- UITextView定义了一个文本视图,显示多行可编辑文本。
Although these classes actually can support the display of arbitrary amounts of text, labels and text fields are intended to be used for relatively small amounts of text, typically a single line. Text views, on the other hand, are meant to display large amounts of text.
虽然这些类实际上可以支持显示任意数量的文本,但UILabel和UITextField旨在用于相对较少量的单行文本,而UITextView可以显示多行文本。
Text view objects, created from the UITextView class, display text formatted into paragraphs, columns, and pages, with all the characteristics of fine typesetting, such as kerning, ligatures, sophisticated line-breaking, and justification. These typographic services are supplied to UITextView through an underlying technology called Text Kit, a powerful layout engine that is both easy to use and extensible. See Using Text Kit to Draw and Manage Text for more information about Text Kit.
UITextView创建的文本视图可以显示格式化的段落,列和页面的文本,具有先进排版的方式,例如字距调整,连字,复杂的划线和对齐。Text Kit的底层技术为UITextView的排版提供支持。它是一种强大的布局引擎,既易于使用又可扩展。有关Text Kit的更多信息,请参阅使用文本工具包绘制和管理文本。
Figure 1-1 shows examples of the primary text objects as they appear on screen. The image on the left shows several different styles of text fields while the image on the right shows a single text view. The callouts displayed on the background are UILabel objects embedded inside the table cells used to display the different views. (These examples were taken from the UIKit Catalog (iOS): Creating and Customizing UIKit Controls sample app, which demonstrates many of the views and controls available in UIKit.)
主要的Text Object如图1-1所示。 左侧的图像显示几种不同类型的文本字段,而右侧的图像显示单个文本视图。 显示在背景上的标注是UILabel对象嵌入在表格单元格中,用于显示不同的视图。 (这些示例来自UIKit目录(iOS):创建和自定义UIKit控件示例应用程序,其中演示了UIKit中可用的许多视图和控件。)

When working with editable text fields and text views, you should always provide a delegate object to manage the editing session. Text views send several different notifications to the delegate to let them know when editing begins, when it ends, and to give them a chance to override some editing actions. For example, the delegate can decide if the current text contains a valid value and prevent the editing session from ending if it does not. When editing does finally end, you also use the delegate to get the resulting text value and update your app’s data model.
当使用可编辑的文本字段和文本视图时,我们通过代理来管理编辑过程。 Text View向代理发送几个不同的通知,以便在编辑开始时结束时让他们知道,并给予他们覆盖某些编辑操作的机会。 例如,代理可以决定当前文本是否包含有效值,如果没有,则阻止编辑会话结束。 编辑完成后,您还可以使用委托来获取结果文本值并更新应用程序的数据模型。
Because there are slight differences in their intended usage, the delegate methods for each text view are slightly different. A delegate that supports the UITextField class implements the methods of the UITextFieldDelegate protocol. Similarly, a delegate that supports the UITextView class implements the methods of the UITextViewDelegate protocol. In both cases, you are not required to implement any of the protocol methods, but if you do not, the text field or view is not as useful.
因为用途稍不同,所以每个文本视图的委托方法也有不同。支持UITextField类的委托实现了UITextFieldDelegate协议的方法。类似地,支持UITextView类的委托实现了UITextViewDelegate协议的方法。在这两种情况下,您都不需要实现任何协议方法,但如果没有,则文本字段或视图不是有用的。
Managing Text Fields and Text Views describes the sequence of delegation messages for both text fields and text views and discusses various tasks performed by the delegates of these objects. For more information about the methods of the UITextFieldDelegate and UITextViewDelegate protocols, see UITextFieldDelegate Protocol Reference and UITextViewDelegate Protocol Reference.
管理文本字段和文本视图叙述了TextField和TextView执行代理的顺序,并讨论了这些对象的代理执行的各种任务。有关UITextFieldDelegate和UITextViewDelegate协议的方法的更多信息,请参阅UITextFieldDelegate Protocol Reference和UITextViewDelegate Protocol Reference。
Use Web Views to Display Web Content(使用WebView显示网页内容)
A web view object displays web-based content. It is an instance of the WKWebView class that enables you to integrate what is essentially a miniature web browser into your app’s user interface. The WKWebView class makes full use of the same web technologies used to implement Safari in iOS, including full support for HTML, CSS, and JavaScript content. The class also supports many of the built-in gestures that users are familiar with in Safari. For example, you can double-click and pinch to zoom in and out of the page and you can scroll around the page by dragging your finger.
Web视图对象显示基于Web的内容。 它是WKWebView类的一个实例,使您能够将基本上是微型Web浏览器的内容集成到应用程序的用户界面中。 WKWebView类充分利用了用于在iOS中实现Safari的相同的Web技术,包括完全支持HTML,CSS和JavaScript内容。 该类还支持用户在Safari中熟悉的许多内置手势。 例如,您可以双击并捏住以放大和缩小页面,您可以通过拖动手指滚动页面。
In addition to displaying content, you can also use a web view object to gather input from the user through the use of web forms. Like the other text classes in UIKit, if you have an editable text field on a form in your web page, tapping that field brings up a keyboard so that the user can enter text. Because it is an integral part of the web experience, the web view itself manages the displaying and dismissing of the keyboard for you.
除了显示内容之外,您还可以使用Web视图对象通过使用Web表单收集用户的输入。像UIKit中的其他文本类一样,如果您的网页上的表单上有一个可编辑的文本字段,点击该字段会弹出一个键盘,以便用户输入文本。因为它是Web体验的组成部分,因此网页视图本身会管理键盘的显示和关闭。
A web view provides information about when pages are loaded, and whether there were any load errors, through its associated delegate object. A web delegate is an object that implements one or more methods of the WKUIDelegate or WKNavigationDelegate protocols. Your implementations of the delegate methods can respond to failures or perform other tasks related to the loading of a web page.
Web视图提供有关何时加载页面的信息,以及是否存在任何加载错误,通过其关联的委托对象。 Web代理是实现WKUIDelegate或WKNavigationDelegate协议的一种或多种方法的对象。您的代理方法的实现可以响应故障或执行与加载网页相关的其他任务。
网友评论