[Text Relatives]

  With TextKit the resources at your disposal range from framework objects—such as text views, text fields, and web views—to text layout engines that you can use directly to draw, lay out, and otherwise manage text. Underlying the text views in UIKit is a powerful layout engine called Text Kit.

  TextKit 让为我们提供了各种objects以及layout engine(渲染排版用). 下图是常用排版术语:

  

  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.

  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. 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 differentnotifications to the delegate to let them know when editing begins, when it ends, and to give them a chance to override some editing actions. 

  Characters and glyphs do not have a one-to-one correspondence. In some cases a character may be represented by multiple glyphs, such as an “é” which may be an “e” glyph combined with an acute accent glyph “´”. In other cases, a single glyph may represent multiple characters, as in the case of a ligature, or joined letter. Figure 2-2 shows individual characters and the single-glyph ligature often used when they are adjacent.

   The glyphs used to depict characters are selected by the layout manager during composition and layout processing. The layout manager determines which glyphs to use and where to place them in the display, or view. The layout manager caches the glyph codes in use and provides methods to convert between characters and glyphs and between characters and view coordinates.

[The Sequence of Messages to the Delegate]

  The following code listings use a date-formatter object to illustrate the use of formatters.

 - (void)viewDidLoad {
[super viewDidLoad];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setGeneratesCalendarDates:YES];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setCalendar:[NSCalendar autoupdatingCurrentCalendar]];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormatter setDateStyle:NSDateFormatterShortStyle]; // example: 4/13/10
DOB.placeholder = [NSString stringWithFormat:@"Example: %@", [dateFormatter stringFromDate:[NSDate date]]]; // code continues....
}

  Using an NSDateFormatter object to convert a date string to a date object.

 - (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
if ([textField.text isEqualToString:@""])
return;
switch (textField.tag) {
case DOBField:
NSDate *theDate = [dateFormatter dateFromString:textField.text];;
if (theDate)
[inputData setObject:theDate forKey:MyAppPersonDOBKey];
break;
// more switch case code here...
default:
break;
}
}

[Using Overlay Views in Text Fields]

  Overlay views are small views inserted into the left and right corners of a text field. They act as controls when users tap them (frequently they are buttons) and act on the current contents of the text field. Searching and bookmarking are two common tasks for overlay views, but others are possible. This overlay view loads a web browser using the (partial) URL in the text field:

  

  Displaying an overlay view in a text field:  

 - (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag == NameField && self.overlayButton) {
textField.leftView = self.overlayButton;
textField.leftViewMode = UITextFieldViewModeAlways;
}
} @dynamic overlayButton; - (UIButton *)overlayButton {
if (!overlayButton) {
overlayButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
UIImage *overlayImage = [UIImage imageNamed:@"bookmark.png"];
if (overlayImage) {
[overlayButton setImage:overlayImage forState:UIControlStateNormal];
[overlayButton addTarget:self action:@selector(bookmarkTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
}
return overlayButton;
}

[Displaying Web Content]

  Loading a local PDF file into the web view:

 - (void)viewDidLoad {
[super viewDidLoad]; NSString *thePath = [[NSBundle mainBundle] pathForResource:@"iPhone_User_Guide" ofType:@"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:@"application/pdf"
textEncodingName:@"utf-8" baseURL:nil];
}
}

  The web-view delegate managing network loading:

 - (void)webViewDidStartLoad:(UIWebView *)webView
{
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
} - (void)webViewDidFinishLoad:(UIWebView *)webView
{
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
} - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; // report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
@"<html><center><font size=+5 color='red'>
An error occurred:<br>%@</font></center></html>",
error.localizedDescription];
[self.myWebView loadHTMLString:errorString baseURL:nil];
}

[Managing the Keyboard]

  The UITextField and UITextView classes both conform to theUITextInputTraits protocol, which defines the properties for configuring the keyboard. When the keyboard is shown or hidden, iOS sends out the following notifications to any registered observers:

  When asked to display the keyboard, the system slides it in from the bottom of the screen and positions it over your app’s content. Because it is placed on top of your content, it is possible for the keyboard to be placed on top of the text object that the user wanted to edit. When this happens, you must adjust your content so that the target object remains visible.

[Copy, Cut, and Paste Operations]   

Several classes and an informal protocol of the UIKit framework give you the methods and mechanisms you need to implement copy, cut, and paste operations in your app:

  • The UIPasteboard class provides pasteboards: protected areas for sharing data within an app or between apps. The class offers methods for writing and reading items of data to and from a pasteboard.

  • The UIMenuController class displays an edit menu above or below the selection to be copied, cut, or pasted into. The default commands of the edit menu are (potentially) Copy, Cut, Paste, Select, and Select All. You can also add custom menu items to the edit menu (see “Adding Custom Items to the Edit Menu”).

  • The UIResponder class declares the method canPerformAction:withSender:. Responder classes can implement this method to show and remove commands of the edit menu based on the current context.

  • The UIResponderStandardEditActions informal protocol declares the interface for handling copy, cut, paste, select, and select-all commands. When users tap one of the commands in the edit menu, the corresponding UIResponderStandardEditActions method is invoked.

  Pasteboards may be public or private. Public pasteboards are called system pasteboards; private pasteboards are created by apps, and hence are called app pasteboards. Pasteboards must have unique names. UIPasteboard defines two system pasteboards, each with its own name and purpose:

  Typically you use one of the system-defined pasteboards, but if necessary you can create your own app pasteboard using pasteboardWithName:create: If you invoke pasteboardWithUniqueNameUIPasteboard gives you a uniquely-named app pasteboard. You can discover the name of a pasteboard through its nameproperty.

最新文章

  1. hdu 1005
  2. 嵌入式 arm平台ping域名指定ip小结
  3. Umbraco(3) - CSS &amp; Javascript(翻译文档)
  4. codevs 3123 高精度练习之超大整数乘法
  5. Stamps and Envelope Size
  6. 网络测试工具netperf
  7. Python 执行字符串表达式函数(eval exec execfile)
  8. markdown 自己搞一个浏览工具
  9. bzoj4785 [Zjoi2017]树状数组
  10. 分红包算法Java实现
  11. Java参数是值传递还是引用传递?
  12. 内链接、左右连接、union并集
  13. C# 类&amp;结构体&amp;枚举
  14. solr中的一些常见错误
  15. HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
  16. flask再学习-重构!启动!
  17. Java中Enum的使用
  18. 工作流2013 assign to问题
  19. B. Black Square(字符串)
  20. 如何学习MySQL

热门文章

  1. TypeScript学习笔记(七) - 命名空间
  2. CH3301 同余方程
  3. mysql update 没有where 不能更新的安全保护设置
  4. 64位windows 2003和windows xp
  5. 黄聪:VS2010编辑C#未启动,打开设计视图时报&quot;未将对象引用设置到对象的实例&quot;
  6. CodeForces - 983B XOR-pyramid(区间dp,异或)
  7. linux shell 修改文本 sed
  8. delete,truncate,drop的区别
  9. VCF文件导入导出
  10. http://bbs.csdn.net/topics/392028373