TTTAttributedLabel 的点击方法

本文详细介绍了如何在iOS应用中利用Objective-C与Swift编写代码实现内链跳转功能,包括创建链接、设置点击事件以及跳转到指定页面的过程。通过实例演示,帮助开发者掌握iOS应用内的页面跳转技巧。

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

str 为某一字符串

NSString *str = RowArr[indexPath.row];
                NSRange allRange = NSMakeRange(0, str.length);
                NSRange range = NSMakeRange(0, 3);
                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
                
                UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:15];
                CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
                [attributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, str.length)];
                
                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xD94448] range:allRange];
                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0X5E5E5E] range:range];
                
                cell.orderInfoLabel.delegate = self;
                [cell.orderInfoLabel addLinkToURL:[NSURL URLWithString:@"买家"] withRange:NSMakeRange(3, str.length)];
                cell.orderInfoLabel.attributedText = attributedString;

事件点击事件的方法

#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Message" bundle:nil];
    BubbleChatViewController *bubbleChatVC = [storyboard instantiateViewControllerWithIdentifier:@"BubbleChatVC"];
    
    NSString *str = [[label.text componentsSeparatedByString:@":"] objectAtIndex:0];
    if ([str isEqualToString:@"买家"]) {
        bubbleChatVC.userID = self.order.buyer.userID;
    } else {
        bubbleChatVC.userID = self.order.opus.opusAuthor.userID;
    }
    [self.navigationController pushViewController:bubbleChatVC animated:YES];
}


转载于:https://my.oschina.net/pingAds/blog/504254

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值