UIKit-UITextView

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 操作

对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的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UITextViewUIKit框架中的一个控件,可以用来显示和编辑长文本。而富文本则是指带有丰富样式的文本,可以设置文字的字体、颜色、大小、间距、行高等属性。 要在UITextView中实现富文本,首先需要创建一个NSAttributedString对象,并通过NSMutableAttributedString来设置文字的样式。NSAttributedString是不可变的,而NSMutableAttributedString可以修改和添加样式。 创建NSMutableAttributedString对象后,可以使用其方法来设置文字的样式,比如设置字体可以使用NSFontAttributeName属性,设置颜色可以使用NSForegroundColorAttributeName属性,设置字号可以使用NSFontAttributeName属性,设置段落样式可以使用NSParagraphStyleAttributeName属性等等。 设置完成后,就可以将NSMutableAttributedString对象赋值给UITextView的attributedText属性,以实现富文本的显示。 例如,我们想将某个UITextView的文字样式设置为红色、字号为20、字体为粗体,可以按如下方式设置: ``` NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"这是富文本"]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributedString.length)]; [attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, attributedString.length)]; textView.attributedText = attributedString; ``` 通过上述代码,就可以在UITextView中显示带有红色、字号为20、字体为粗体的文字。 除了以上示例外,UITextView还支持更多的富文本样式设置,根据具体需求,可以设置更多的属性来实现更丰富的文本效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值