一、简介
< <<继承关系:UITextField-->UIScrollView-->UIView-->UIResponder-->NSObject < 格式为 1-->初始化(作用) typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) { UIDataDetectorTypePhoneNumber = 1 << 0, // Phone number detection UIDataDetectorTypeLink = 1 << 1, // URL detection UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // Street address detection UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3, // Event detection UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // Shipment tracking number detection UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 5, // Flight number detection UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 6, // Information users may want to look up UIDataDetectorTypeNone = 0, // Disable detection UIDataDetectorTypeAll = NSUIntegerMax // Enable all types, including types that may be added later } __TVOS_PROHIBITED;(如果属性有枚举类型的话,这里会有枚举类型说明) UITextView *textView = [[UITextView alloc] init]; (这是具体的例子) @property(null_resettable,nonatomic,copy) NSString *text;// UITextField 设置提示文字 (这是说明) 二、UITextView的文本属性(属性的顺序与苹果API一致) 1-->设置UITextView Delegate代理 textView.delegate = self;//设置textView Delegate代理 @property(nullable,nonatomic,weak) id 2-->设置文字 textView.text = @"反馈内容(最多可输入120字)";//设置文字 @property(nullable, nonatomic,copy) NSString *text;//设置显示文字, 默认是空的 3-->设置字号//一般方法 textView.font = [UIFont systemFontOfSize:30]; @property(null_resettable, nonatomic,strong) UIFont *font;// 设置字体 4-->文字字体加粗//系统加粗方法 [textView setFont:[UIFont boldSystemFontOfSize:25]]; 5-->自定义文字字体和字号 textView.font = [UIFont fontWithName:@"Zapfino"size:30]; 6-->自定义文字字体加粗和字号 [textView setFont:[UIFont fontWithName:@"Helvetica-Bold"size:25]]; 7-->设置文字颜色 textView.textColor = [UIColor redColor];//设置文字颜色 @property(nullable, nonatomic,strong) UIColor *textColor; 8-->文字对齐方式 typedef NS_ENUM(NSInteger, NSTextAlignment) { NSTextAlignmentLeft = 0, // 居左对齐 #if TARGET_OS_IPHONE NSTextAlignmentCenter = 1, //居中对齐 NSTextAlignmentRight = 2, // 居右对齐 #else /* !TARGET_OS_IPHONE */ NSTextAlignmentRight = 1, //居右对齐 NSTextAlignmentCenter = 2, //居中对齐 #endif NSTextAlignmentJustified = 3, //合理铺满 等同于居左 NSTextAlignmentNatural = 4, //默认 等同于居左 } textView.textAlignment = NSTextAlignmentCenter; @property(nonatomic) NSTextAlignment textAlignment;// 对齐方式,默认是NSLeftTextAlignment 默认都是竖直居中的 UITextView不能设置竖直方向的排列布局,但是可以通过sizeToFit改变UITextView的frame来实现曲线救国。 9-->设置选中范围 是否可以编辑 textView.selectedRange = NSMakeRange(0,0);//当UITextView中含有文字时,系统默认将光标定位到最后的位置,设置光标定位到首位置。 @property(nonatomic) NSRange selectedRange;//设置光标定位的位置 10-->设置是否可以编辑 textView.editable = NO; // 默认YES @property(nonatomic,getter=isEditable) BOOL editable __TVOS_PROHIBITED;//设置是否可以编辑,默认YES(TVOS 禁止使用) 11-->是否可以选中 textView.selectable = NO; // 默认YES 当设置为NO时,不能选择@property(nonatomic,getter=isSelectable) BOOL selectable;//切换可选择性,它控制用户选择内容和与url和附件交互的能力。在tvOS上,这也使得文本视图可聚焦。 12-->设置显示数据类型的连接模式 typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) { UIDataDetectorTypePhoneNumber = 1 << 0, //检测电话 UIDataDetectorTypeLink = 1 << 1, //检测网址和邮箱 UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // 检测地址 UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3, // 检测日历 UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // 货物追踪号码检测 UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 5, // 班机号码检测 UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 6, //用户可能要查找的信息 UIDataDetectorTypeNone = 0, // 禁用检测 UIDataDetectorTypeAll = NSUIntegerMax // 检测所有类型链接 } __TVOS_PROHIBITED; textview.dataDetectorTypes=UIDataDetectorTypePhoneNumber; // 检测电话号码 @property(nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;//设置检测的类型。 详见使用UIDataDetectorTypes自动检测电话、网址和邮箱 13-->设置是否允许编辑属性字符串文本 textView.allowsEditingTextAttributes = YES; // 默认NO @property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // 默认为NO 14-->设置富文本 NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];-->先把UITextField上的文字赋值给可变字符串 [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5,10)];-->设置更改后的颜色和改变文字的区域 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(20, 25)];-->设置更改后的字体大小和改变文字的区域 textView.attributedText = str;-->把改后的字符串重新赋值给UITextField @property(null_resettable,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); //更改任意文字的颜色和字体大小 15-->设置属性字典 NSMutableDictionary * attributesDic = [textView.typingAttributes mutableCopy]; [attributesDic setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName]; // automatically resets when the selection changes // 重新设置 接下来改变的文字 的属性字典 textView.typingAttributes = attributesDic; @property(nullable, nonatomic,copy) NSDictionary*typingAttributes NS_AVAILABLE_IOS(6_0); //.(当选中文本时,自动复位)适用于用户输入的新文本的属性。字典包含适用于新类型文本的属性键(和对应的值)。当文本字段的选择更改时,自动清除字典的内容。如果文本字段不是编辑模式,此属性包含值为零。类似地,除非文本字段当前处于编辑模式,否则不能将此属性赋值给该属性 16-->滚动到文本的某个段落 [textView scrollRangeToVisible:NSMakeRange(50, 5)]; - (void)scrollRangeToVisible:(NSRange)range;//滚动textView使其显示在本一段文本。 17-->当文本字段成为第一响应者时,自定义输入视图显示。 UIImageView *imgView1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"[email protected]"]]; imgView1.frame=CGRectMake(60, 60, 300, 300); textView.inputView=imgView1; @property (nullable, readwrite, strong) UIView *inputView;//只有height值会对视图有影响,只会改变附加视图的高度,弹出添加的这个视图,一般用作像银行app的自定义键盘 18-->当文本字段成为第一响应者时,该自定义辅助视图显示。 UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 50)]; view.backgroundColor = [UIColor redColor]; // 在键盘上附加一个视图,一般用于添加一个收回键盘的按钮 textView.inputAccessoryView = view; @property (nullable, readwrite, strong) UIView *inputAccessoryView;// 在键盘上附加一个视图,一般用于添加一个收回键盘的按钮 19-->设置是否显示删除按钮 textView.clearsOnInsertion = YES; // 默认为NO @property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0); // 默认为没有。如果是,选择UI是隐藏的,插入文本将替换字段的内容。更改选择将自动设置为NO. 三、设置UITextView的初始化方法 1-->父类方法(没有初始frame) UITextView * textView = [[UITextView alloc] init]; 2-->父类方法(初始frame) UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 80, 300, 200)]; 3-->UITextView 的一个类方法 NSTextContainer *textContainer =[[NSTextContainer alloc]init]; UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 80, 300, 200) textContainer:container]; - (instancetype)initWithFrame:(CGRect)frame textContainer:(nullable NSTextContainer *)textContainer NS_AVAILABLE_IOS(7_0) NS_DESIGNATED_INITIALIZER; 4-->UITextView的一个类方法 - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER; 详见 initWithNibName、initWithCoder、awakeFromNib和 loadNibNamed详解 四、UITextView的textContainer属性 1-->定义了一个矩形区域用于存放已经进行了排版并设置好属性的文字(只读属性) NSTextContainer *textContainer=textView.textContainer; @property(nonatomic,readonly) NSTextContainer *textContainer NS_AVAILABLE_IOS(7_0);/ /获取文本视图的文本容器 2-->设置显示内容的填充 textView.textContainerInset=UIEdgeInsetsMake(0, 10, 0, 10); @property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0);//在文本视图的内容区域内设置文本容器的布局区域 五、UITextView的布局属性 1-->用于管理NSTextStorage其中的文字内容的排版布局(只读属性) NSInteger characterIndex=[textView.layoutManager characterIndexForPoint:touchPoint inTextContainer:textView.textContainer fractionOfDistanceBetweenInsertionPoints:NULL]; @property(nonatomic,readonly) NSLayoutManager *layoutManager NS_AVAILABLE_IOS(7_0);// 方便访问器(通过文本容器访问) 2-->NSTextStorage保存并管理UITextView要展示的文字内容,该类是NSMutableAttributedString的子类,由于可以灵活地往文字添加或修改属性(只读属性) TextStorage *textStorage=textView.textStorage; @property(nonatomic,readonly,strong) NSTextStorage *textStorage NS_AVAILABLE_IOS(7_0); 3-->设置链接文本的样式设置 NSDictionary*linkAttributes =@{NSForegroundColorAttributeName: [UIColor greenColor], NSUnderlineColorAttributeName: [UIColor lightGrayColor], NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)}; textView.linkTextAttributes=linkAttributes; @property(null_resettable, nonatomic, copy) NSDictionary *linkTextAttributes NS_AVAILABLE_IOS(7_0); 六、UITextView的UITextViewDelegate代理方法 可选方法 1-->将要开始编辑 #pragma mark - UITextViewDelegate代理方法 /** 将要开始编辑 @param textView UITextView对象 @return YES:允许编辑; NO:禁止编辑 */ - (BOOL)textViewShouldBeginEditing:(UITextView*)textView{ return YES; } - (BOOL)textViewShouldBeginEditing:(UITextView*)textView; //.当文本将要开始编辑时调用这个代理方法,返回 NO 时驳回编辑状态。 2-->是否结束编辑 /** 将要结束编辑 应用场景:如果当前textView有内容,则返回YES,允许收键盘或更换textView; 当前textView没有输入内容,返回NO,此时不能收起键盘或者更换textView @param textView UITextView对象 @return YES:允许释放键盘(注销第一响应者); NO:不允许释放键盘(始终是第一响应者) */ - (BOOL)textViewShouldEndEditing:(UITextView*)textView{ //返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder //要想在用户结束编辑时阻止文本字段消失,可以返回NO //这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息 return YES; } - (BOOL)textViewShouldEndEditing:(UITextView*)textView;//./当文本将要结束编辑时进行调用,返回 YES 时允许编辑停止或者注销第一响应者,返回 NO,不允许编辑会话结束 3-->开始编辑时调用的方法 /** 开始编辑,即成为第一响应者,此时光标出现 @param textView UITextView对象 */ - (void)textViewDidBeginEditing:(UITextView*)textView{ //开始编辑时触发,文本字段将成为first responder } - (void)textViewDidBeginEditing:(UITextView*)textView; //当文本正在开始编辑时调用,变为第一响应者 4-->结束编辑调用的方法 /** 已经结束编辑 (即使shouldEndEditing方法返回NO或者调用了endEditing:YES,该方法仍可能调用) 官方注释:may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called @param textView UITextView对象 */ - (void)textViewDidEndEditing:(UITextView*)textView{ //结束编辑时触发 } - (void)textViewDidEndEditing:(UITextView*)textView;//上面返回YES后执行;上面返回NO时有可能强制执行(e.g.view removed from window) 5-->是否允许字符改变(控制输入文字的长度和内容,可通调用以下代理方法实现) - (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{ if(range.location>=100){ //控制输入文本的长度 returnNO; } if([textisEqualToString:@"\n"]) { //禁止输入换行 returnNO; } else{ returnYES; } } - (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text;//内容将要发生改变编辑 6-->字符内容改变触发的方法 - (void)textViewDidChange:(UITextView*)textView{ //计算文本的高度 CGSizeconstraintSize; constraintSize.width= textView.frame.size.width-16; constraintSize.height=MAXFLOAT; CGSizesizeFrame =[textView.textsizeWithFont: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); } - (void)textViewDidChange:(UITextView*)textView;//内容发生改变编辑 7-->选中内容改变触发的方法 - (void)textViewDidChangeSelection:(UITextView *)textView{ NSLog(@"textViewDidChangeSelection:"); } - (void)textViewDidChangeSelection:(UITextView *)textView; 8-->是否允许对文本中的URL进行操作 - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { if([[URL scheme] isEqualToString:@"url1"]) { NSString* url =[URL host]; NSLog(@"%@",url);//在这里利用url做点什么事情...... return NO; } return YES; } - (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange; 8-->是否允许对文本中的富文本进行操作 - (BOOL)textView:(UITextView*)textView shouldInteractWithTextAttachment:(NSTextAttachment*)textAttachment inRange:(NSRange)characterRange{ NSLog(@"附件被点击"); return YES; } - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0); 九、UITextField的通知 UITextView派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextView类特有的事件 UITextViewTextDidBeginEditingNotification UITextViewTextDidChangeNotification UITextViewTextDidEndEditingNotification 当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。 因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知 UIKeyboardWillShowNotification //键盘显示之前发送 UIKeyboardDidShowNotification //键盘显示之后发送 UIKeyboardWillHideNotification //键盘隐藏之前发送 UIKeyboardDidHideNotification //键盘隐藏之后发送 十、UITextView的UITextViewDelegate代理方法拓展 1-->UITextView:响应键盘的 return事件 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if([text isEqualToString:@"\n"]){//判断输入的字是否是回车,即按下return //在这里做你响应return键的代码 [textView resignFirstResponder]; returnNO;//这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行 } return YES; } 2-->textView自适应输入的文本的内容的高度 - (void)textViewDidChange:(UITextView*)textView{ //计算文本的高度 CGSizeconstraintSize; constraintSize.width= textView.frame.size.width-16; constraintSize.height=MAXFLOAT; CGSizesizeFrame =[textView.textsizeWithFont: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); } 3-->控制输入文字的长度和内容,可通调用以下代理方法实现 - (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{ if(range.location>=100){ //控制输入文本的长度 returnNO; } if([textisEqualToString:@"\n"]) { //禁止输入换行 returnNO; } else { returnYES; } } 3、UITextView中打开或禁用复制,剪切,选择,全选等功能 // 继承UITextView重写这个方法 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { // 返回NO为禁用,YES为开启 // 粘贴 if(action == @selector(paste:)) returnNO; // 剪切 if(action == @selector(cut:)) returnNO; // 复制 if(action == @selector(copy:)) returnNO; // 选择 if(action == @selector(select:)) returnNO; // 选中全部 if(action == @selector(selectAll:)) returnNO; // 删除 if(action == @selector(delete:)) returnNO; // 分享 if(action == @selector(share)) returnNO; return [super canPerformAction:action withSender:sender]; }