iOS界面编程-UITextView

一、介绍

   继承关系: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;


控件自适应输入的文本的内容的高度,可在textViewDidChange的代理方法中加入调整控件大小的代理即可

- (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];
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章