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: 方法时,每次修改都会触发文本存储对象的更新和布局,可能会带来一定的性能开销
UITextView的问题
UITextView并不会自动根据内容撑开,除非你将.isScrollEnabled = false.此时他会自动撑开你的view。
意思是:在viewWillLayoutSubviews的情况下,或者如果你直接调用
self.view.setNeedLayout
self.view.layoutIfNeeded
会发现UITextView的宽度,高度是0。因为他没办法知道自己该多高
那解决办法是:你需要用sizeThatFit去获取这个textView应该多高,再给他设置高度约束。
enumerateAttribute
enumerateAttribute:inRange:options:usingBlock: 是 NSAttributedString 类中的一个方法,用于遍历指定范围内的属性,并执行指定的 block 操作
对tableView行高做处理
设置了第一个section是每行行高60,第二个section,第一行40,第二行30,并且这个是UITableViewStylePlain类型
UITableViewStyleGrouped类型
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {//设置第一个section的行高都是60
return 60;
} else {
if (indexPath.row == 0) {
return 40;
} else {
return 30;
}
}
}
对tableView footer/header view设置
/**
对头尾视图进行设置
*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
view.backgroundColor = [UIColor redColor];
return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
view.backgroundColor = [UIColor blueColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
对cell进行编辑
通过edit btn按钮来触发编辑状态,编辑可以是插入或者删除
- (void)editBtnClick
{
if (self.tableView.isEditing) {
self.tableView.editing = NO;
} else {
self.tableView.editing = YES;
}
}
/**设置编辑风格,就是是删除还是插入**/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleInsert;
}
}
/**
点击编辑的删除或者插入会触发下面这个方法
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"看这里%s",__func__);
}
添加索引
具体跳转可以自定义通过- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"A",@"B",@"C",@"D"];
}
//自定义索引跳转方式,例如点第一个跳转最后一个
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return (tableView.numberOfSections - 1)-index;
}
复用问题
dequeueReusableCellWithIdentifier方法会
问题
UITableViewStylePlain和UITableViewStyleGrouped的区别