iOS富文本Label实现点击事件,类似Word在横线上输入编辑

iOS富文本Label实现点击事件,类似Word在横线上输入编辑,第1张

.h

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.简单显示label

YYLabel *label = [YYLabel new]

label.frame = CGRectMake(100,50,100,25)

label.font = [UIFont systemFontOfSize:14.0f]

label.textColor = [UIColororangeColor]

label.textAlignment=NSTextAlignmentCenter

label.lineBreakMode=0

label.numberOfLines = NSLineBreakByWordWrapping

label.text=@"YYTextDemo Test"

//    [self.view addSubview:label]

//2.属性字符串 简单实用

NSMutableAttributedString*text= [[NSMutableAttributedStringalloc] initWithString:@"上海市第九城市信息技术有限公司"]

text.font = [UIFontboldSystemFontOfSize:13.0f]

text.color = [UIColorblueColor]

[textsetColor:[UIColor redColor]range:NSMakeRange(0,4)]

//    给你的label添加点击事件

[textsetTextHighlightRange:NSMakeRange(0,4)

color:[UIColor orangeColor]

backgroundColor:[UIColorwhiteColor]

tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){

NSLog(@"tap text range:...")

}]

YYLabel *attributedLabel = [YYLabel new]

attributedLabel.frame = CGRectMake(100,100,160,25)

attributedLabel.attributedText = text

attributedLabel.userInteractionEnabled=YES

attributedLabel.backgroundColor = [UIColor purpleColor]

//    [self.view addSubview:attributedLabel]

//3.图文混排模式

NSMutableAttributedString *textImage = [NSMutableAttributedString new]

UIFont *font= [UIFont systemFontOfSize:14.0f]

inti =2

switch(i) {

case0:

{

//    方式一

NSString*title =@"2006年的诺贝尔文学奖颁给了土耳其作家奥尔罕.帕慕克。在很多人都很意外的时候,我心头却感到一阵开心,因为早在两年前,我就知道了这个作家"

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]]

UIImage *image = [UIImage imageNamed:@"babilogo"]

image= [UIImage imageWithCGImage:image.CGImage scale:5orientation:UIImageOrientationUp]

NSMutableAttributedString*attachText= [NSMutableAttributedStringattachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]

[textImage appendAttributedString:attachText]

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:@"凭借《我的名字叫红》获得了都柏林文学..."attributes:nil]]

break

}

case1:

{

NSString *title=@"text 与 UIView混排:"

[textImageappendAttributedString:[[NSAttributedStringalloc] initWithString:title attributes:nil]]

UISwitch *switcher = [UISwitch new]

switcher.frame= CGRectMake(0,0,50,50)

[switcher sizeToFit]

NSMutableAttributedString*attachText = [NSMutableAttributedString attachmentStringWithContent:switcher contentMode:UIViewContentModeCenter attachmentSize:switcher.frame.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]

[textImageappendAttributedString:attachText]

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"attributes:nil]]

break

}

case2:

{

NSString*title =@"2006年的诺贝尔文学奖颁给了土耳其作家奥尔罕.帕慕克。在很多人都很意外的时候,我心头却感到一阵开心,因为早在两年前,我就知道了这个作家"

[textImage appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]]

UIImageView *imageView = [UIImageView new]

imageView.frame= CGRectMake(0,0,20,20)

[imageViewsetImage:[UIImage imageNamed:@"babilogo"]]

imageView.contentMode =UIViewContentModeScaleAspectFill

imageView.userInteractionEnabled=YES

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id_Nonnullsender) {

NSLog(@"ImageAction")

}]

[imageViewaddGestureRecognizer:tap]

NSMutableAttributedString*attachText = [NSMutableAttributedStringattachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.frame.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]

[textImageappendAttributedString:attachText]

NSString*subTitle=@"凭借《我的名字叫红》获得了都柏林文学,2006年的诺贝尔文学奖颁给了土耳其作家奥尔罕.帕慕克。在很多人都很意外的时候,我心头却感到一阵开心,因为早在两年前,我就知道了这个作家"

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:subTitle attributes:nil]]

NSMutableParagraphStyle*paragraphStyle= [[NSMutableParagraphStylealloc] init]

[paragraphStylesetLineSpacing:16]//调整行间距

[textImage addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [title length]+[subTitle length])]

[textImage setTextHighlightRange:NSMakeRange(0,8)

color:[UIColor orangeColor]

backgroundColor:[UIColor whiteColor]

tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){

NSLog(@"tap text range:...")

}]

break

}

default:

break

}

textImage.font = font

YYLabel *textImageLabel = [YYLabel new]

textImageLabel.userInteractionEnabled =YES

textImageLabel.numberOfLines =0

textImageLabel.frame = CGRectMake(0,0,320,400)

textImageLabel.attributedText = textImage

//    [self.view addSubview:textImageLabel]


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11813848.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存