
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
{
}
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, listV.frame.size.width - 60, listV.frame.size.height)]
l.userInteractionEnabled=YES
UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)]
[l addGestureRecognizer:labelTapGestureRecognizer]
-(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
UILabel *label=(UILabel*)recognizer.view
NSLog(@"%@被点击了",label.text)
源码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条)