一、介紹
繼承關係:NSObject 《-UIResponder《-UIView《-UIScrollView《-UITextView,UITextView可以輸入多行文字,並且繼承自scrollview表明還可以滑動顯示。iOS6之後可以使用attributedText屬性,爲一段文字設置多種類型。
二、相關方法和屬性
1、初始化方法,創建一個textview
- (instancetype nonnull)initWithFrame:(CGRect)frame textContainer:(NSTextContainer * nullable)textContainer
2、配置文字屬性
@property(nonatomic, copy) NSString * __null_unspecified text
@property(copy) NSAttributedString * __null_unspecified attributedText
@property(nonatomic, strong) UIFont *font 字體
@property(nonatomic, strong) UIColor *textColor 文字顏色
@property(nonatomic, getter=isEditable) BOOL editable 設置UITextView是否可以編輯
@property(nonatomic) BOOL allowsEditingTextAttributes
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes
@property(nonatomic) NSTextAlignment textAlignment
@property(nonatomic, copy) NSDictionary <NSString *,id> *typingAttributes
@property(nonatomic, copy) NSDictionary <NSString *,id> * __null_unspecified linkTextAttributes
3.選擇操作
@property(nonatomic) NSRange selectedRange
- (void)scrollRangeToVisible:(NSRange)range 滑動到指定區域
@property(nonatomic) BOOL clearsOnInsertion
@property(nonatomic, getter=isSelectable) BOOL selectable
4、訪問委託
@property(nonatomic, weak) id< UITextViewDelegate > delegate
5、訪問text kit對象@property(nonatomic, readonly) NSLayoutManager *layoutManager
@property(nonatomic, readonly) NSTextContainer *textContainer
@property(nonatomic, readonly, strong) NSTextStorage *textStorage
三、實際例子
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, textField.frame.origin.y + 31 + 10, self.view.frame.size.width - 20, 80)];
textView.backgroundColor = [UIColor redColor];
textView.text =@"test hello lil i\ns a label!this is a l\nabel!this this this is a label!this is a label!this is a label!this is a \n\nlabel!this this this is a \nlabel!this is a label!!this this this is a label!this is a label!this is a label!this is a label!this this this is a label!this is a labely and lucyafsffasdf";
textView.textColor = [UIColor blackColor];
textView.textAlignment = NSTextAlignmentRight;
textView.editable = YES;
textView.selectedRange = NSMakeRange(0, 5);
textView.delegate = self;
[textView scrollRangeToVisible:NSMakeRange(190, 5)];
textView.keyboardType = UIKeyboardTypeEmailAddress;
textView.clearsOnInsertion = YES;
textView.returnKeyType = UIReturnKeyGo;
// textView.selectable = YES;
// textView.inputView = button;
textView.inputAccessoryView = button;
[self.view addSubview:textView];
UITextViewDelegate方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;<span style="font-family: Arial, Helvetica, sans-serif;">//將要開始編輯</span>
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;<span style="font-family: Arial, Helvetica, sans-serif;">//將要結束編輯</span>
- (void)textViewDidBeginEditing:(UITextView *)textView; <span style="font-family: Arial, Helvetica, sans-serif;">//開始編輯</span>
- (void)textViewDidEndEditing:(UITextView *)textView;<span style="font-family: Arial, Helvetica, sans-serif;">//結束編輯</span>
//內容將要發生改變編輯
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
//內容發生改變編輯
- (void)textViewDidChange:(UITextView *)textView;
//焦點發生改變
- (void)textViewDidChangeSelection:(UITextView *)textView;
- (void)textViewDidChange:(UITextView *)textView{
//計算文本的高度
CGSize constraintSize;
constraintSize.width = textView.frame.size.width-16;
constraintSize.height = MAXFLOAT;
CGSize sizeFrame =[textView.text sizeWithFont:textView.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
//重新調整textView的高度
textView.frame =CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,sizeFrame.height+5);
}
控制輸入文字的長度和內容,可通調用以下代理方法實現
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.location>=100)
{
//控制輸入文本的長度
return NO;
}
if ([text isEqualToString:@"\n"]) {
//禁止輸入換行
return NO;
}
else
{
return YES;
}
}
UITextView退出鍵盤的幾種方式
1)如果你程序是有導航條的,可以在導航條上面加多一個Done的按鈕,用來退出鍵盤,當然要先實UITextViewDelegate。
- (void)textViewDidBeginEditing:(UITextView *)textView {
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(dismissKeyBoard)];
self.navigationItem.rightBarButtonItem = done;
[done release];
done = nil;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)dismissKeyBoard {
[self.textView resignFirstResponder];
}
2)如果你的textview裏不用回車鍵,可以把回車鍵當做退出鍵盤的響應鍵。
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
3)還有你也可以自定義其他加載鍵盤上面用來退出,比如在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。
代碼如下:
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:self
action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = @[btnSpace, doneButton];;
[topView setItems:buttonsArray];
[textView setInputAccessoryView:topView];//當文本輸入框加上topView
-(IBAction)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}