placeHolder
在文本框未输入时显示的text
[self.textFiled setPlaceholder:@"这是placeHolder"];
securityEntry
变成密码框类型
[self.textFiled setSecureTextEntry:YES];
clearButton
是否显示右边叉号
[self.textFiled setClearButtonMode:UITextFieldViewModeWhileEditing];
textStorage
它代表文本容器的存储对象,用于管理和操作文本内容
主要是通过访问UITextView.textStorage来管理UITextView相关样式
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
// 设置文本
textView.text = @"Hello, world!";
// 获取 textStorage
NSTextStorage *textStorage = textView.textStorage;
// 修改文本样式
[textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
// 插入文本
[textStorage replaceCharactersInRange:NSMakeRange(7, 0) withString:@"iOS"];
很明显上面这部分操作直接使用textView本体就能完成
直接使用 UITextView 的 text 属性和相关方法可能更简单和方便。textStorage 更适用于需要更高级编辑和样式控制的情况,如自定义文本布局、特殊效果的应用等。
beginEditing和endEditing
在调用此方法之后,对文本进行的任何修改都将被视为一个编辑事务。
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"Hello, world!"];
[textStorage beginEditing];
// 对文本进行修改
[textStorage replaceCharactersInRange:NSMakeRange(0, 5) withString:@"Hi"];
[textStorage setAttributes:@{
NSForegroundColorAttributeName: [UIColor redColor]} range:NSMakeRange(0, 5)];
[textStorage endEditing];
作用!!
使用 beginEditing 和 endEditing 方法可以减少系统处理多个编辑操作时的开销。它们可以帮助系统在开始编辑之后进行一次性的更新和布局,而不是在每个单独的修改调用之后进行处理。
直接调用 replaceCharactersInRange:withString: 方法时,每次修改都会触发文本存储对象的更新和布局,可能会带来一定的性能开销
enumerateAttribute
enumerateAttribute:inRange:options:usingBlock: 是 NSAttributedString 类中的一个方法,用于遍历指定范围内的属性,并执行指定的 block 操作