iOS UITextView详解 陌生属性解释,添加展示超链接并交互

么么哒
 

@property(nonatomic,copy) NSDictionary<NSAttributedStringKey, id> *typingAttributes NS_AVAILABLE_IOS(6_0); // 可以设置一些属性付给textview,比如:(设置行距)

    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = 20;// 字体的行间距
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
                                 NSParagraphStyleAttributeName:paragraphStyle};
    textView.typingAttributes = attributes;

@property (nullable, readwrite, strong) UIView *inputView;//inputView就是显示键盘的view,重写这个view,就不会弹出键盘,而是弹出你写的这个view

@property (nullable, readwrite, strong) UIView *inputAccessoryView;//附着在键盘顶部的一个自定义view,直接初始化一个view  赋给这个属性即可。

@property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0);//貌似能自动获取焦点弹出键盘,并且如果textview中有文字时,会全选文字,可删除 赋值等。

@property(nonatomic,readonly) NSTextContainer *textContainer NS_AVAILABLE_IOS(7_0);//点击查看详细解释

@property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0);//文本内容的内边距

@property(nonatomic,readonly) NSLayoutManager *layoutManager NS_AVAILABLE_IOS(7_0);

@property(null_resettable, nonatomic, copy) NSDictionary<NSString *, id> *linkTextAttributes NS_AVAILABLE_IOS(7_0);//链接的样式字典 配合它的attributedText属性使用。为uitextview添加超链接,并交互点击查看详细使用方法

@property(nonatomic,readonly,strong) NSTextStorage *textStorage NS_AVAILABLE_IOS(7_0);
//NSTextStore 
ps:UITextView的textStorage属性继承自NSMutibleAttributeString,
所以,你可以通过修改textStorage的值来更改UItextView某一字段的字体或颜色。例如 [self.myTextView.textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
这将UITextView的前面三个字符的字体改成红色。

 

UITextView的代理方法如下:


//将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView*)textView;

//将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;

//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;

//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView;

//内容将要发生改变编辑
- (BOOL)textView:(UITextView *)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView;

//焦点发生改变
- (void)textViewDidChangeSelection:(UITextView*)textView;

//处理URL点击交互
//用textView:shouldInteractWithURL:inRange:forInteractionType:替代
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange;;

//用textView:shouldInteractWithTextAttachment:inRange:forInteractionType:替代
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange;

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);

 

textview的三个通知:

UIKIT_EXTERN NSNotificationName const UITextViewTextDidBeginEditingNotification;
UIKIT_EXTERN NSNotificationName const UITextViewTextDidChangeNotification;
UIKIT_EXTERN NSNotificationName const UITextViewTextDidEndEditingNotification;

 

有时候我们要控件自适应输入的文本的内容的高度,只要在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 *)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (range.location>=100){
       //控制输入文本的长度
      return NO;
    }
    if ([text isEqualToString:@"\n"]) {
      //禁止输入换行
      returnNO;
    }else{
      return YES;
    }
}

-- NORMAL --

-- NORMAL --

-- NORMAL --

-- NORMAL --
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值