蘋果對UITextView的解釋:UITextView displays a region that can contain multiple lines of editable text. When a user taps a text view, a keyboard appears; when a user taps Return in the keyboard, the keyboard disappears and the text view can handle the input in an application-specific way. You can specify attributes, such as font, color, and alignment, that apply to all text in a text view.
大概意思為:UITextView可以編輯多行文本,當UITextView成為第一響應者的時候(進入編輯模式彈出鍵盤)可以編輯文本。結束編輯的時候可以可以根據自己所設定的特定的文本方式處理響應的文本(包括字型,大小,對其方式,顏色....)
屬性:
text:文本
font:字型
textColor:字型顏色
textAlignment:對齊 (預設向靠左對齊)
selectedRange:選擇範圍(有兩個屬性location:游標的位置 length:選中的文本的長度)
editable:是否允許編輯(預設是YES允許編輯)
selectable:是否允許選擇(預設允許 ios7加入)
dataDetectorTypes:資料檢測類型(取值為:UIDataDetectorTypes一共有6種類型:UIDataDetectorTypePhoneNumber:電話文本連結;UIDataDetectorTypeLink:URL連結;UIDataDetectorTypeAddress:地址連結;UIDataDetectorTypeCalendarEvent:日曆類型連結;UIDataDetectorTypeNone:沒有類型;UIDataDetectorTypeAll:所有類型)只有在不能編輯的情況下這些才能被識別
allowsEditingTextAttributes:允許編輯富文本(預設不允許iOS6加入)
attributedText:富文本
typingAttributes:富文字格式設定
inputView:輸入視圖(效果可參考UITextfield)
inputAccessoryView:輸入輔助視圖 (效果可參考UITextfield)
clearsOnInsertion:是否能修改之前的文本(預設為不能)
textContainer:文本容器(一個矩形地區用於存放已經進行了排版並設定好屬性的文字 iOS7加入)
textContainerInset:文本視圖的縮排範圍(及文本地區的布局iOS7加入)
layoutManager:管理NSTextStorage其中的文字內容的排版布局 (iOS7加入)
textStorage:儲存並管理UITextView要展示的文字內容(是NSMutableAttributedString的一個子類iOS7加入)
linkTextAttributes:連結文本的屬性(iOS7 加入)
方法:
- (void)scrollRangeToVisible:(NSRange)range;滾動TextView使其顯示指定地區的文字
- (instancetype)initWithFrame:(CGRect)frame textContainer:(nullable NSTextContainer *)textContainer ;重新指定文本[內容] 檢視的位置內容(iOS7加入)
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder ;反歸檔
代理:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView; 是否允許textview進入編輯狀態
- (BOOL)textViewShouldEndEditing:(UITextView *)textView; 是否允許textview結束編輯狀態
- (void)textViewDidBeginEditing:(UITextView *)textView; textview進入編輯狀態後執行的方法(成為第一響應者)
- (void)textViewDidEndEditing:(UITextView *)textView; textview結束編輯狀態執行的方法(取消第一響應者)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; 是否響應改變當前文本(預設可以該改變,及當前輸入的文本有效)
- (void)textViewDidChange:(UITextView *)textView; textview的常值內容發生改變的時候執行的方法
- (void)textViewDidChangeSelection:(UITextView *)textView; 焦點發生改變執行的方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange; 是否能夠給textview添加超連結及點擊超連結執行的代理方法(返回YES進入超連結)
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange ; 點擊富文本中的圖片執行的代理法方
通知:
UITextViewTextDidBeginEditingNotification; 開始編輯觸發
UITextViewTextDidChangeNotification; 常值內容發生改變觸發
UITextViewTextDidEndEditingNotification; 結束編輯觸發