// 设置 UILabel 内容和 URL 颜色的方法
- (void)setLabel:(UILabel *)label withText:(NSString *)text name:(NSString *)name urlColor:(UIColor *)urlColor nameColor:(UIColor *)nameColor {
[label setUserInteractionEnabled:YES];
// 初始化 URL 范围数组
self.urlRanges = [NSMutableArray array];
// 将普通字符串转换为 NSMutableAttributedString
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
// 设置用户名颜色
NSRange nameRange = [text rangeOfString:name];
if (nameRange.location != NSNotFound) {
[attributedString addAttribute:NSForegroundColorAttributeName value:nameColor range:nameRange];
}
// 正则表达式匹配 URL
NSString *urlPattern = @"(https?://[a-zA-Z0-9./]+)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlPattern options:0 error:nil];
NSArray<NSTextCheckingResult *> *matches = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)];
// 遍历匹配结果,并设置 URL 的颜色和点击属性
for (NSTextCheckingResult *match in matches) {
NSRange urlRange = match.range;
// 确保 URL 范围在 attributedString 范围内
if (NSMaxRange(urlRange) <= attributedString.length) {
[attributedString addAttribute:NSForegroundColorAttributeName value:urlColor range:urlRange];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:urlRange];
// 存储 URL 范围和内容
NSString *urlString = [text substringWithRange:urlRange];
[self.urlRanges addObject:@{@"url": urlString, @"range": [NSValue valueWithRange:urlRange]}];
} else {
NSLog(@"URL range out of bounds for attributed string. Skipping range: %@", NSStringFromRange(urlRange));
}
}
// 设置 label 的 attributedText
label.attributedText = attributedString;
// 添加点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
[label addGestureRecognizer:tap];
}
// 点击手势处理方法
- (void)labelTapped:(UITapGestureRecognizer *)gesture {
UILabel *label = (UILabel *)gesture.view;
CGPoint location = [gesture locationInView:label];
// 创建用于识别点击位置的 layoutManager、textContainer 和 textStorage
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
// NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:label.bounds.size];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:label.intrinsicContentSize];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:label.attributedText];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
// 配置 textContainer
textContainer.lineFragmentPadding = 0.0;
textContainer.lineBreakMode = label.lineBreakMode;
textContainer.maximumNumberOfLines = label.numberOfLines;
// 找到点击的字符索引
NSInteger characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textContainer
fractionOfDistanceBetweenInsertionPoints:nil];
NSLog(@"location = x=%f y=%f", location.x, location.y);
NSLog(@"characterIndex = %ld", (long)characterIndex);
// 判断是否点击到 URL 区域
for (NSDictionary *urlInfo in self.urlRanges) {
NSRange urlRange = [urlInfo[@"range"] rangeValue];
// 打印检查
NSLog(@"Checking URL range: %@ for characterIndex: %ld", NSStringFromRange(urlRange), (long)characterIndex);
if (NSLocationInRange(characterIndex, urlRange)) {
NSString *urlString = urlInfo[@"url"];
NSLog(@"URL 点击: %@", urlString);
[self openURL:[NSURL URLWithString:urlString]];
break;
}
}
}
// 打开 URL 的方法
- (void)openURL:(NSURL *)url {
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
}
UILabel.text 中有两个URL 设置颜色 和点击时间
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...
- (2018-06-09-周六 10:48:06) Are you sure you want to refresh...
- FloatingActionButton 问题:如果页面中有两个以上的浮动按钮,在退出页面时候出现异常multip...
- “宰相刘罗锅”一词是在同名电视剧的热播而被大家熟知的,和铁齿铜牙纪晓岚很相似,宰相刘罗锅的形象也深入人心,殊不知这...
- 关于青春的电影现在有很多,但是好看的并不多。 例如大家熟知的《那些年》,又或是《致青春》,再就是《同桌的你》。。 ...