Objective-C属性字符串NSAttributedString

本文介绍了如何使用系统提供的NSAttributedString类来创建富文本,并在Objective-C中实现点击事件功能。通过UITextView展示文本,利用NSFontAttributeName设置字体,NSParagraphStyleAttributeName调整段落样式。示例代码展示了点击事件的实现,当点击特定文字时,能携带参数并触发相应操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSAttributedString是Objective-C中的属性字符串类,GitHub上也有很多第三方,用得较多的是TTTAttributedLabel,这里给大家介绍一下系统NSAttributedString类来实现富文本,并可实现点击事件,同时点击事件可携带参数。
因为要做点击事件,所以我们用UITextView,首先声明一个UITextView属性:

@property (nonatomic, strong) UITextView *textView;

NSAttributedString中NSFontAttributeName是用来设置文字字体的,有很多可以设置的属性,这里介绍一些常用的,其他的用到的话修改一下key-value就可以了:

//NSFontAttributeName:文字字体
- (void)setAttributeCorlorAndSize{
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 100)];
    _textView.editable = NO;
    _textView.selectable = NO;
    _textView.scrollEnabled = NO;
    _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_textView];

    //初始化属性字符串
    NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:@"富文本:文字颜色 字体大小 背景色 下划线 空心 点击事件"];
    //文字颜色
    [aAttributedString addAttribute:NSForegroundColorAttributeName
                              value:[UIColor redColor]
                              range:NSMakeRange(4, 4)];

    //文字大小
    [aAttributedString addAttribute:NSFontAttributeName
                              value:[UIFont systemFontOfSize:18]
                              range:NSMakeRange(9, 4)];


    //文字背景色
    [aAttributedString addAttribute:NSBackgroundColorAttributeName
                              value:[UIColor blueColor]
                              range:NSMakeRange(14, 3)];

    //下划线  NSUnderlineColorAttributeName设置下划线颜色
    [aAttributedString addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:1]
                        range:NSMakeRange(18, 3)];

    //空心
    [aAttributedString addAttribute:NSStrokeWidthAttributeName
                              value:[NSNumber numberWithInt:3]
                              range:NSMakeRange(22, 2)];
    //点击事件携带一个NSInteger类型,值为110的参数
    [aAttributedString addAttribute:@"tapID" value:[NSNumber numberWithInteger:110] range:NSMakeRange(25, 4)];

    [_textView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textTapped:)]];


    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //行间距
    paragraphStyle.lineSpacing = 15.0;

    [aAttributedString addAttribute:NSParagraphStyleAttributeName
                             value:paragraphStyle
                             range:NSMakeRange(0, aAttributedString.length)];



    _textView.attributedText = aAttributedString;
}

点击事件实现:

-(void)textTapped:(UITapGestureRecognizer*)recognizer{

    NSLayoutManager *layoutManager = _textView.layoutManager;
    CGPoint location = [recognizer locationInView:_textView];
    location.x -= _textView.textContainerInset.left;
    location.y -= _textView.textContainerInset.top;

    NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
                                                      inTextContainer:_textView.textContainer
                             fractionOfDistanceBetweenInsertionPoints:NULL];

    if(characterIndex < _textView.textStorage.length){
        NSRange range;
        id number = [_textView.attributedText attribute:@"tapID" atIndex:characterIndex effectiveRange:&range];
        NSNumber *value_id = number;

        NSLog(@"%@", value_id);
    }
}

NSParagraphStyleAttributeName用来设置段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)

- (void)setAttributeParagraph{

    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, SCREEN_WIDTH, 200)];
    label2.numberOfLines = 0;
    [self.view addSubview:label2];

    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSParagraphStyleAttributeName 段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)"];
    //段落样式
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //段落间距
    paragraphStyle.paragraphSpacing = 20.0;

    [attributedString addAttribute:NSParagraphStyleAttributeName
                                          value:paragraphStyle
                                          range:NSMakeRange(0, attributedString.length)];

         label2.attributedText = attributedString;
}

效果图,点击事件就不演示了,但点击“点击事件”四个字的时候,控制台打印所携带的参数110。
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值