NS_ASSUME_NONNULL_BEGIN
@interface GHAttributesLabel : UILabel
typedef void(^GHAttributesBlock)(NSRange poinRange)
/**
@param text 传入富文本类型的字符串
@param actionText 要响应事件的字符串
*/
/**
点击事件回调
*/
@property (nonatomic , copy) GHAttributesBlock actionBlock
.m
//
// GHAttributesLabel.m
// GHAttributesLabelDemo
//
// Created by zhaozhiwei on 2019/1/20.
// Copyright © 2019年 GHome. All rights reserved.
//
@interface GHTextView : UITextView
@end
@implementation GHTextView
@end
@interface GHAttributesLabel()<UITextViewDelegate>
@property (nonatomic , strong) GHTextView *textView
@property (nonatomic , copy) NSString *actionText
/** <#注释#>*/
@property (nonatomic, assign) NSRange range
@end
@implementation GHAttributesLabel
//return YES
}
@end
使用
-(void)test{
GHAttributesLabel *attributesLabel = [[GHAttributesLabel alloc]initWithFrame:CGRectMake(10, 200, [UIScreen mainScreen].bounds.size.width - 20, 250)]
//NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:temp]
// NSRange range = [temp rangeOfString:actionStr]
//NSLog(@"range%@",NSStringFromRange(range))
NSArray *actionArr = [self rangeOfSubString:actionStr inString:temp]
NSLog(@"===:%@",[self rangeOfSubString:actionStr inString:temp])
for (int i = 0i <actionArr.counti++) {
NSValue *value = actionArr[i]
NSRange actionRange = [value rangeValue]
[attrStr addAttribute:NSLinkAttributeName
value:actionStr
range: actionRange]
}
}
}
return rangeArray
}
-(NSMutableAttributedString *)keyWords:(NSString *)keyWords withKeyWordsColor:(UIColor *)color
{
}
基本实现思路就是利用正则识别链接加YYLabel设置链接点击事件,下面说下这过程中遇到的问题1、自动布局YYLabel后发现设置label.numberOfLines = 0后不换行,YYLabel还得设置一个preferredMaxLayoutWidth属性,这个属性是设置最大宽度,设置完才能有换行功能
2、添加YYLabel的分类并且利用正则识别链接
3、通过给NSAttributedString设置高亮并且添加点击事件
4、添加完点击事件后点击发现并没有响应,是因为label的父类试图添加了点击手势导致的手势冲突解决办法是给父试图的手势添加代理并且实现代理方法
源码WPAttributedMarkup, WPAttributedMarkup能给Label中某关键字添加文字效果或点击事件。Label中的某个关键字词可以改变字体的特性如颜色、加粗,下划线等,也可以为某个关键字词添加点击事件。
<ignore_js_op>
使用方法:
通过创建不同的style的字典后就可以对label的属性进行设置,示例如下:
// Example using fonts and colours
NSDictionary* style1 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:18.0],
@"bold":[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
@"red": [UIColor redColor]}
// Example using arrays of styles, dictionary attributes for underlining and image styles
NSDictionary* style2 = @{@"body" :
@[[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
[UIColor darkGrayColor]],
@"u": @[[UIColor blueColor],
@{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}
],
@"thumb":[UIImage imageNamed:@"thumbIcon"] }
// Example using blocks for actions when text is tapped. Uses the 'link' attribute to style the links
NSDictionary* style3 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:22.0],
@"help":[WPAttributedStyleAction styledActionWithAction:^{
NSLog(@"Help action")
}],
@"settings":[WPAttributedStyleAction styledActionWithAction:^{
NSLog(@"Settings action")
}],
@"link": [UIColor orangeColor]}
self.label1.attributedText = [@"AttributedBoldRedtext" attributedStringWithStyleBook:style1]
self.label2.attributedText = [@"[td]Multiplestylestext[td]" attributedStringWithStyleBook:style2]
<br/>
本文来源于<b>大学IT网</b>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)