美文网首页
OC内容输入篇(三)UITextView

OC内容输入篇(三)UITextView

作者: oceanfive | 来源:发表于2018-08-11 14:52 被阅读11次

    [TOC]

    属性

    通用的属性

    @property(null_resettable,nonatomic,copy) NSString *text;
    @property(nullable,nonatomic,strong) UIFont *font;
    @property(nullable,nonatomic,strong) UIColor *textColor;
    @property(nonatomic) NSTextAlignment textAlignment;    // default is NSLeftTextAlignment
    @property(null_resettable,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0);
    

    代理

    @property(nullable,nonatomic,weak) id<UITextViewDelegate> delegate;
    

    具体如下:

    @protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>
    
    @optional
    
    // 允许编辑
    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
    // 允许结束编辑
    - (BOOL)textViewShouldEndEditing:(UITextView *)textView;
    // 开始编辑了
    - (void)textViewDidBeginEditing:(UITextView *)textView;
    // 结束编辑了
    - (void)textViewDidEndEditing:(UITextView *)textView;
    // 允许输入
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
    // 输入了内容的:这个是`UITextField`没有的,需要使用通知处理
    - (void)textViewDidChange:(UITextView *)textView;
    // 选中的内容改变:这个是`UITextField`没有的
    - (void)textViewDidChangeSelection:(UITextView *)textView;
    // 链接 url 的处理:这个是`UITextField`没有的
    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);
    // Attachment 的处理:这个是`UITextField`没有的
    - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);
    
    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");
    - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithTextAttachment:inRange:forInteractionType: instead");
    
    @end
    

    选中

    // 选中的内容范围
    @property(nonatomic) NSRange selectedRange;
    // 是否可以选中
    @property(nonatomic,getter=isSelectable) BOOL selectable NS_AVAILABLE_IOS(7_0); // toggle selectability, which controls the ability of the user to select content and interact with URLs & attachments. On tvOS this also makes the text view focusable.
    

    UITextInput协议也有@property (nullable, readwrite, copy) UITextRange *selectedTextRange;,是UITextRange类型的,类型不一致

    可编辑的

    @property(nonatomic,getter=isEditable) BOOL editable __TVOS_PROHIBITED;
    
    • YES: 可以弹出键盘,输入内容
    • NO: 不可以弹出键盘,不可以输入内容

    注意
    UITextFieldenabled属性,因为UITextField继承于UIControl

    数据内容类型

    @property(nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
    

    取值如下:

    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;
    

    注意:
    1、需要把editable设置为NO才会有效果,这时候是没有办法通过键盘输入的
    2、点击相应的UIDataDetectorTypes会进行相应的操作,比如url会使用Safari浏览器打开;
    3、可以添加和拦截富文本attchment的操作


    url属性处理如下:

        textview.editable = NO;
        textview.dataDetectorTypes = UIDataDetectorTypeAll;
        textview.text = @"https://www.baidu.com 呵呵呵";
        textview.linkTextAttributes = @{NSBackgroundColorAttributeName: [UIColor redColor], NSUnderlineStyleAttributeName: @3};
    

    url的点击跳转操作可以在代理方法中拦截到:

    // 10.0 之前
    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
        return YES;
    }
    // 10.0 之后
    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
        return YES;
    }
    

    返回YES,可以跳转; 返回NO不可以跳转;

    允许编辑富文本

    @property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // defaults to NO
    
    • allowsEditingTextAttributes : 会有加粗bold斜体italic下划线underline的操作

    UITextField也有这个属性

    输入中的内容属性

    @property(nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes
    
    • typingAttributes : 用来控制输入的时候,输入的文字的属性,比如斜体,下划线等等;The attributes to apply to new text being entered by the user.

    • 如果选中了或者选中范围发生变化,属性会被清空nil ;
    • 只能在编辑中的状态进行属性赋值和读取,不在编辑中的状态获取到的内容为nil;

    UITextField也有这个属性

    滚动

    // 滚动到 range 范围
    - (void)scrollRangeToVisible:(NSRange)range;
    

    清除

    @property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0); // defaults to NO. if YES, the selection UI is hidden, and inserting text will replace the contents of the field. changing the selection will automatically set this to NO.
    

    UITextField也有这个属性

    textContainerInset

    文字内边距

    @property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0);
    

    默认是 {8, 0, 8, 0}


    @property(nonatomic,readonly) NSTextContainer *textContainer NS_AVAILABLE_IOS(7_0);
    @property(nonatomic,readonly) NSLayoutManager *layoutManager NS_AVAILABLE_IOS(7_0);
    @property(nonatomic,readonly,strong) NSTextStorage *textStorage NS_AVAILABLE_IOS(7_0);
    
    • textContainer对应的是绘制文字内容的容器

    contentInset

    内边距,增加了滚动范围

    @property(nonatomic)  UIEdgeInsets   contentInset;
    

    链接

    @property(null_resettable, nonatomic, copy) NSDictionary<NSString *, id> *linkTextAttributes NS_AVAILABLE_IOS(7_0);
    

    如果内容是链接的属性,可以自定义带下划线,斜体等等

    通知

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

    最后

    GitHub参考链接

    相关文章

      网友评论

          本文标题:OC内容输入篇(三)UITextView

          本文链接:https://www.haomeiwen.com/subject/ibddbftx.html