UILabel设置属性字符串,可以增加点击事件点击

该博客介绍如何利用TextView替代UILabel,设置属性字符串并添加点击事件。通过这种方法,可以方便地为不同部分的文本设置不同的样式,并实现点击交互。


1.首先啊,label可以设置属性字符串,设置不同的字体颜色,字体大小,但是要增加点击事件,不好操作。

2.所以文章是设置textView来完成的。


- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
//        NSMutableAttributedString *attributedString = [NSString attributuStringWithString:self.descText lineSpacing:10];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1.您可能很久未更新最新数据导致图表有误,请及时刷新;\n以上分析根据公积金官网数据统计分析所得,可能存在有误。"];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"ProvidentFundshuaxin://"
                                 range:[[attributedString string] rangeOfString:@"刷新"]];
        [attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} range:[self.descText rangeOfString:@"刷新"]];
        _textView.attributedText = attributedString;
        _textView.linkTextAttributes = @{NSForegroundColorAttributeName: ColorFromHexRGB(0xe73c00),
                                         NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
        
        _textView.textColor = ColorFromHexRGB(0xa0a0a0);
        _textView.delegate = self;
        _textView.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘
        _textView.scrollEnabled = NO;
        
//        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
//        paragraphStyle.lineSpacing = 20;// 字体的行间距
//        NSDictionary *attributes = @{
//                                     NSFontAttributeName:[UIFont systemFontOfSize:12],
//                                     NSParagraphStyleAttributeName:paragraphStyle
//                                     };
//        _textView.typingAttributes = attributes;
//                        _textView.layer.borderColor = [UIColor cyanColor].CGColor;
//                        _textView.layer.borderWidth = 1.0f;
    }
    return _textView;
}


textView代理事件

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    NSRange range = [@"1.您可能很久未更新最新数据导致图表有误,请及时刷新;\n以上分析根据公积金官网数据统计分析所得,可能存在有误。" rangeOfString:@"刷新"];
    if (NSEqualRanges(characterRange, range)) {
        NSLog(@"设置您的自定义事件");
//        if (self.refreshAccountBlock) {
//            self.refreshAccountBlock();
//        }
    }
    return YES;
}

ok 完美解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值