OC内容输入篇(一)相关协议

协议的继承关系

- NSObject
    - UITextInputTraits
        - UIKeyInput
            - UITextInput

协议:UITextInputTraits

属性

autocapitalizationType

@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;

自动首字母大写的类型;

  • UITextAutocapitalizationTypeNone : 没有首字母大写,输入的什么就是什么
  • UITextAutocapitalizationTypeWords : 单词的首字母大写
  • UITextAutocapitalizationTypeSentences : 句子的首字母大写, (默认的)
  • UITextAutocapitalizationTypeAllCharacters : 所有的输入字母都是大写的,对于一些专业术语名词有用,比如United States.,是US

autocorrectionType

自动纠错功能,根据键盘类型,有的实现了,有的没有实现

@property(nonatomic) UITextAutocorrectionType autocorrectionType;
  • UITextAutocorrectionTypeDefault : 默认的
  • UITextAutocorrectionTypeNo: 没有
  • UITextAutocorrectionTypeYes: 有

spellCheckingType

@property(nonatomic) UITextSpellCheckingType spellCheckingType NS_AVAILABLE_IOS(5_0);

拼写的检查类型:

  • UITextSpellCheckingTypeDefault
  • UITextSpellCheckingTypeNo
  • UITextSpellCheckingTypeYes

smartQuotes 智能引号

@property(nonatomic) UITextSmartQuotesType smartQuotesType NS_AVAILABLE_IOS(11_0); // default is UITextSmartQuotesTypeDefault;
@property(nonatomic) UITextSmartDashesType smartDashesType NS_AVAILABLE_IOS(11_0); // default is UITextSmartDashesTypeDefault;
@property(nonatomic) UITextSmartInsertDeleteType smartInsertDeleteType NS_AVAILABLE_IOS(11_0); // default is UITextSmartInsertDeleteTypeDefault;

enablesReturnKeyAutomatically

// 自动打开/禁用返回键return
@property(nonatomic) BOOL enablesReturnKeyAutomatically;

没有内容时,禁用 return 键,不可以点击;
有内容时,打开 return 键,可以点击;

returnKeyType

@property(nonatomic) UIReturnKeyType returnKeyType;

取值有:

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
    UIReturnKeyDefault,
    UIReturnKeyGo,
    UIReturnKeyGoogle,
    UIReturnKeyJoin,
    UIReturnKeyNext,
    UIReturnKeyRoute,   // 路线
    UIReturnKeySearch,
    UIReturnKeySend,
    UIReturnKeyYahoo,
    UIReturnKeyDone,
    UIReturnKeyEmergencyCall,  // 紧急电话
    UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0), // 继续
};

keyboardType

键盘类型

@property(nonatomic) UIKeyboardType keyboardType;

取值如下:

typedef NS_ENUM(NSInteger, UIKeyboardType) {
    UIKeyboardTypeDefault,                // Default type for the current input method.
    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters
    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

};
  • UIKeyboardTypeASCIICapable : 英文加数字
  • UIKeyboardTypeNumbersAndPunctuation: 数字符号
  • UIKeyboardTypeNumberPad : 数字九宫格键盘,带有删除
  • UIKeyboardTypePhonePad : 电话键盘,有一些字符+*#
  • UIKeyboardTypeDecimalPad : 和 UIKeyboardTypeNumberPad 一样,多了字符点 .

keyboardAppearance

键盘外观样式

@property(nonatomic) UIKeyboardAppearance keyboardAppearance;

取值有:

typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
    UIKeyboardAppearanceDefault,          // Default apperance for the current input method.
    UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
    UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
    UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // Deprecated
};
  • UIKeyboardAppearanceDark : 黑色的
  • UIKeyboardAppearanceLight : 亮色的

secureTextEntry

安全输入

@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;
  • UITextField : 显示安全输入,别人看不到输入的内容,无法复制输入内容
  • UITextView : 不会显示安全输入,无法复制输入内容

Setting this property to YES in any view that conforms to UITextInputTraits disables the user’s ability to copy the text in the view.

textContentType

@property(nonatomic,copy) UITextContentType textContentType NS_AVAILABLE_IOS(10_0); // default is nil

输入的内容类型,根据这个值会自动调整键盘类型

取值如下:

UIKIT_EXTERN UITextContentType const UITextContentTypeName                      NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeNamePrefix                NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeGivenName                 NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeMiddleName                NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeFamilyName                NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeNameSuffix                NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeNickname                  NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeJobTitle                  NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeOrganizationName          NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeLocation                  NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeFullStreetAddress         NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeStreetAddressLine1        NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeStreetAddressLine2        NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeAddressCity               NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeAddressState              NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeAddressCityAndState       NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeSublocality               NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeCountryName               NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypePostalCode                NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeTelephoneNumber           NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeEmailAddress              NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeURL                       NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeCreditCardNumber          NS_AVAILABLE_IOS(10_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeUsername                  NS_AVAILABLE_IOS(11_0);
UIKIT_EXTERN UITextContentType const UITextContentTypePassword                  NS_AVAILABLE_IOS(11_0);

UIKeyInput

#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) BOOL hasText;
#else
- (BOOL)hasText;
#endif
- (void)insertText:(NSString *)text;
- (void)deleteBackward;
  • hasText : 有内容返回YES,没有输入内容返回NO
  • insertText : 输入内容,在光标的位置处输入内容
  • deleteBackward : 删除内容,删除光标位置的前一个内容

注意:
1、使用insertTextdeleteBackward方法输入字符走代理方法- (void)textViewDidChange:(UITextView *)textView
2、直接赋值的方法textview.text = @"helo" 不会走代理方法

UITextInput

方法

获取/替换范围内的内容

// 获取 range 范围内的内容
- (nullable NSString *)textInRange:(UITextRange *)range;
// 使用 text 替换 range 范围内的内容
- (void)replaceRange:(UITextRange *)range withText:(NSString *)text;

选中

@property (nullable, readwrite, copy) UITextRange *selectedTextRange;
  • 不是编辑状态:
    • selectedTextRange: <_UITextKitTextRange: 0x1c4231400> (0, 0)F

补充:
1、光标位置实质上就是选中的文字范围selectedTextRange,只不过是长度为0
2、光标位置从0开始,到length结束,即 [0, length]

临时的,用户还没有确定

marked表示用户还没有确定输入内容,但是已经输入了内容,比如中文输入了拼音,但是没有确定汉字

// 范围
@property (nullable, nonatomic, readonly) UITextRange *markedTextRange; // Nil if no marked text.

// 样式,可以设置背景颜色等等,参考 NSAttribuedString
@property (nullable, nonatomic, copy) NSDictionary *markedTextStyle; // Describes how the marked text should be drawn.

// 设置 marked 的内容,如果存在就替换处理,不存在的话再光标处插入内容
- (void)setMarkedText:(nullable NSString *)markedText selectedRange:(NSRange)selectedRange; // selectedRange is a range within the markedText

// 取消 marked 的内容(有空格),相当于 "确认"(没有空格)
- (void)unmarkText;
  • setMarkedText : 如果selectedRange超过范围不会crash,但是会有错误日志[Assert] requesting caretRectForPosition: with a position beyond the NSTextStorage (4)

注意:
selectedTextRange : 没有选中范围时不为nil,一直是有内容的,(本质上是因为光标存在,所以一直存在)
markedTextRange : 没有待确定的内容是为空nil

位置获取

- (CGRect)firstRectForRange:(UITextRange *)range;
- (CGRect)caretRectForPosition:(UITextPosition *)position;
- (NSArray *)selectionRectsForRange:(UITextRange *)range NS_AVAILABLE_IOS(6_0);       // Returns an array of UITextSelectionRects

不存在的情况:

    "<_UITextKitSelectionRect: 0x1c4249bd0> ({{inf, inf}, {0, 0}})"

补充 UITextSelectionRect

NS_CLASS_AVAILABLE_IOS(6_0) @interface UITextSelectionRect : NSObject

@property (nonatomic, readonly) CGRect rect;
@property (nonatomic, readonly) UITextWritingDirection writingDirection;
@property (nonatomic, readonly) BOOL containsStart; // Returns YES if the rect contains the start of the selection.
@property (nonatomic, readonly) BOOL containsEnd; // Returns YES if the rect contains the end of the selection.
@property (nonatomic, readonly) BOOL isVertical; // Returns YES if the rect is for vertically oriented text.

@end

位置点获取

// 找到点point最近的位置UITextPosition
- (nullable UITextPosition *)closestPositionToPoint:(CGPoint)point;
// 在 range 范围内找到点point最近的位置UITextPosition
- (nullable UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(UITextRange *)range;
// 点 point 所在位置处的字符的范围
- (nullable UITextRange *)characterRangeAtPoint:(CGPoint)point;

代理

/* A system-provied input delegate is assigned when the system is interested in input changes. */
@property (nullable, nonatomic, weak) id <UITextInputDelegate> inputDelegate;

/* A tokenizer must be provided to inform the text input system about text units of varying granularity. */
@property (nonatomic, readonly) id <UITextInputTokenizer> tokenizer;

其他

/* Layout questions. */
- (nullable UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction;
- (nullable UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction;

/* Writing direction */
- (UITextWritingDirection)baseWritingDirectionForPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction;
- (void)setBaseWritingDirection:(UITextWritingDirection)writingDirection forRange:(UITextRange *)range;

UITextPosition 位置

NS_CLASS_AVAILABLE_IOS(3_2) @interface UITextPosition : NSObject

@end

记录内容位置的类,不使用NSInteger

创建,获取

// 开始位置
@property (nonatomic, readonly) UITextPosition *beginningOfDocument;
// 结束位置
@property (nonatomic, readonly) UITextPosition *endOfDocument;

// 根据某个位置获取偏移量的位置
- (nullable UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset;
- (nullable UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset;

比较

// 比较结果
- (NSComparisonResult)comparePosition:(UITextPosition *)position toPosition:(UITextPosition *)other;
// 两个位置的偏移量
- (NSInteger)offsetFromPosition:(UITextPosition *)from toPosition:(UITextPosition *)toPosition;

布局

- (nullable UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction;
- (nullable UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction;

UITextRange 范围

类定义

NS_CLASS_AVAILABLE_IOS(3_2) @interface UITextRange : NSObject

@property (nonatomic, readonly, getter=isEmpty) BOOL empty;     //  Whether the range is zero-length.
@property (nonatomic, readonly) UITextPosition *start;
@property (nonatomic, readonly) UITextPosition *end;

@end

创建

- (nullable UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition;

最后

GitHub参考链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,265评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,274评论 1 288
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,087评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,479评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,782评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,218评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,594评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,316评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,955评论 1 237
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,274评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,803评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,177评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,732评论 3 229
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,953评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,687评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,263评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,189评论 2 258

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,306评论 0 5
  • 不知不觉,岁寒输入法的更新历史已经可以列出这么一长串来了。从中可以看出,岁寒的发展过程也是一个不断试错的过程,其中...
    临岁之寒阅读 33,781评论 1 6
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 98,432评论 9 468
  • Vim几句话介绍Vim是Unix系统上的文本编辑软件(你该不会不知道什么是文本编辑软件吧!),在windows上也...
    何必遠方阅读 3,019评论 0 3
  • 将灵魂力缓缓地注入到了灵魂水晶之中。 几个光亮的斑点,在灵魂水晶里面悬浮,不过灵魂水晶基本是灰暗的。 “我瞬间觉得...
    im喵小姐阅读 3,706评论 0 0