【iOS】—— 富文本实现超链接功能

我们经常会看到很多程序上会有超链接,并且我们点击之后会直接跳转到响应的网页界面,但是我们无法使用普通的UILabel来实现,而是使用UITextView来完成这个操作的,今天就来浅谈一下它的实现。

一、UITextView详解:

1.UITextView简单属性:

	//初始化UITextView
    UITextView *textview =  [[UITextView alloc] initWithFrame:CGRectMake(40, 100, 300, 50)];
    textview.backgroundColor = [UIColor orangeColor]; //设置背景色
    textview.scrollEnabled = YES;    //设置当文字超过视图的边框时是否允许滑动,默认为“YES”
    textview.editable = YES;        //设置是否允许编辑内容,默认为“YES”
    textview.delegate = self;       //设置代理方法的实现类
    textview.font = [UIFont fontWithName:@"Arial" size:18.0]; //设置字体名字和字体大小;
    textview.returnKeyType = UIReturnKeyDefault; //设置return键的类型
    textview.keyboardType = UIKeyboardTypeDefault; //设置键盘类型一般为默认
    textview.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
    textview.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式(如电话号码、网址、地址等)
    textview.textColor = [UIColor blackColor]; // 设置显示文字颜色
    textview.text = @"UITextView详解"; //设置显示的文本内容
    [self.view addSubview:textview];

2.UITextView的代理方法:

//将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;

//将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;

//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;

//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView;

//改变文本前被调用,range中是变化范围,text中是变化后的字符串,如果返回NO,变化将不会被响应
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

//文本变更时被调用
- (void)textViewDidChange:(UITextView *)textView;

//游标移动,选择范围发生变化是被调用
- (void)textViewDidChangeSelection:(UITextView *)textView;

//控件自适应输入的文本的内容的高度,只要在textViewDidChange的代理方法中加入调整控件大小的代理即可
- (void)textViewDidChange:(UITextView *)textView {
    //计算文本的高度
    CGSize constraintSize;
    constraintSize.width = textView.frame.size.width - 16;
    constraintSize.height = MAXFLOAT;
    CGSize sizeFrame = [textView.text sizeWithFont:textView.font
                                constrainedToSize:constraintSize
                                    lineBreakMode:UILineBreakModeWordWrap];
    
    //重新调整textView的高度
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, sizeFrame.height + 5);
}

// 控制输入文字的长度和内容,可调用以下方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (range.location >= 32) {
        //控制输入文本的长度
        return  NO;
    }
    if ([text isEqualToString:@"\n"]) {
        //禁止输入换行
        return NO;
    } else {
        return YES;
    }
}

//textView指定范围的内容与 URL 将要相互作用时激发该方法——该方法随着 IOS7被使用; 
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
 
//textView指定范围的内容与文本附件将要相互作用时,自动激发该方法——该方法随着 IOS7被使用;
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);

从上述的属性及方法我们发现,这个东西具有的功能很多,它就像是UITextFieldUILabel的结合体一样,当然它的🐮🍺还不在这,它还可以进行图文混搭等骚操作,这里就不多说了,不是要说的重点。

二、NSMutableAttributedString简单介绍:

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。之前在网上找了一些资料,有的是重绘UILabeltextLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想。后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现。

1.NSMutableAttributedString的组成:

NSMutableAttributedString它由两部分组成:

  • 文字内容:NSString *
  • 文字属性:NSDictionary *,文字属性为一个字典类型的数据,其可以包含一下内容:

文字颜色 - NSForegroundColorAttributeName
字体大小 - NSFontAttributeName
下划线 - NSUnderlineStyleAttributeName
背景色 - NSBackgroundColorAttributeName

例如:

NSMutableAttributedString *mastring = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:22.0f]}];

2.NSMutableAttributedString初始化:

// 使用字符串初始化
- (instancetype)initWithString:(NSString *)str;

// 使用字符串初始化,attributes存放一些属性名和属性值
- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSAttributedStringKey, id> *)attrs;

// 使用NSAttributedString初始化
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

3.NSMutableAttributedString方法:

//  为某一范围内的文字添加某个属性的方法
- (void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range;

// 为某一范围内的文字添加多个属性的方法
- (void)addAttributes:(NSDictionary<NSAttributedStringKey, id> *)attrs range:(NSRange)range;

// 移除某个范围内的某个属性的方法
- (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range;

4.NSAttributedString文本属性:

属性说明
NSFontAttributeName文本的字体(默认是Helvetica(Neue) 12),对应的值是一个UIFont对象
NSForegroundColorAttributeName文本的字体颜色(默认为黑色),对应的值是一个UIColor对象
NSBackgroundColorAttributeName文本的背景颜色(默认是nil),对应的值是一个UIColor对象
NSLigatureAttributeName指某些连在一起的字符,它们采用单个的图元符号(默认值为1)。0表示没有连体字符。1表示使用默认的连体字符
NSKernAttributeName用于调整字距的像素点数(默认是0),对应的值是一个NSNumber对象(整数)
NSStrikethroughStyleAttributeName指定是否在文字上加上删除线(默认是NSUnderlineStyleNone),对应的值是一个NSNumber对象(整数)
NSStrikethroughColorAttributeName指定删除线颜色
NSUnderlineStyleAttributeName指定是否在文字上加上下划线(默认是NSUnderlineStyleNone),对应的值是一个NSNumber对象(整数)
NSUnderlineColorAttributeName指定下划线颜色
NSStrokeWidthAttributeName改变描边宽度(默认为0),所对应的值是一个NSNumber对象(小数)
NSStrokeColorAttributeName指定描边颜色(默认是NSForegroundColorAttributeName),对应的值是一个UIColor对象
NSShadowAttributeName阴影(默认为nil),所对应的值是一个NSShadow对象
NSTextEffectAttributeName设置文本特殊效果,所对应的值是一个NSString对象,目前只有图版印刷效果可用
NSBaselineOffsetAttributeName设置基线偏移值,取值为NSNumber对象(浮点),正值上偏,负值下偏
NSObliquenessAttributeName设置字形倾斜度,取值为NSNumber对象(浮点),正值右倾,负值左倾
NSExpansionAttributeName设置文本横向拉伸属性,取值为NSNumber(浮点),正值横向拉伸文本,负值横向压缩文本
NSWritingDirectionAttributeName设置文字书写方向,从左向右书写或者从右向左书写
NSVerticalGlyphFormAttributeName设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本
NSLinkAttributeName设置链接属性,点击后调用浏览器打开指定URL地址
NSAttachmentAttributeName设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

NSStrikethroughStyleAttributeNameNSUnderlineStyleAttributeName样式:

样式说明
NSUnderlineStyleNone不设置
NSUnderlineStyleSingle细的单线
NSUnderlineStyleThick粗的单线
NSUnderlineStyleDouble细的双线
NSUnderlineStylePatternSolid线样式是连续的实线
NSUnderlineStylePatternDot线样式是虚线点
NSUnderlineStylePatternDash线样式是破折号
NSUnderlineStylePatternDashDot线样式是破折号和点
NSUnderlineStylePatternDashDotDot线样式是破折号和点点
NSUnderlineStyleByWord有空格的地方不设置

实例代码:

NSMutableAttributedString *fontText = [[NSMutableAttributedString alloc] initWithString:@"正常字体25号粗体"];
[fontText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:25]} range:NSMakeRange(4, 5)];

NSMutableAttributedString *colorText = [[NSMutableAttributedString alloc] initWithString:@"红色字体蓝色背景"];
[colorText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
[colorText addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(4, 4)];
    
NSMutableAttributedString *kernText = [[NSMutableAttributedString alloc] initWithString:@"间距为5间距为10"];
[kernText setAttributes:@{NSKernAttributeName:@5, NSBackgroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, 4)];
[kernText setAttributes:@{NSKernAttributeName:@10, NSBackgroundColorAttributeName:[UIColor blueColor]} range:NSMakeRange(4, 5)];

NSMutableAttributedString *lineLabel = [[NSMutableAttributedString alloc] initWithString:@"删除线下划线"];
[lineLabel addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle|NSUnderlinePatternDot) range:NSMakeRange(0, 3)];
[lineLabel addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];

[lineLabel addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(3, 3)];
[lineLabel addAttribute:NSUnderlineColorAttributeName value:[UIColor blueColor] range:NSMakeRange(3, 3)];

NSMutableAttributedString *strokeText = [[NSMutableAttributedString alloc] initWithString:@"描边2描边4"];
[strokeText addAttribute:NSStrokeWidthAttributeName value:@(2) range:NSMakeRange(0, 3)];
[strokeText addAttribute:NSStrokeWidthAttributeName value:@(4) range:NSMakeRange(3, 3)];
[strokeText addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 4)];

NSMutableAttributedString *shadowText = [[NSMutableAttributedString alloc] initWithString:@"字体阴影"];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor magentaColor];
shadow.shadowOffset = CGSizeMake(10, 5);
[shadowText addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, 4)];

NSMutableAttributedString *baselineOffsetText = [[NSMutableAttributedString alloc] initWithString:@"正值上偏负值下偏"];
[baselineOffsetText addAttribute:NSBaselineOffsetAttributeName value:@(5) range:NSMakeRange(2, 2)];
[baselineOffsetText addAttribute:NSBaselineOffsetAttributeName value:@(-5) range:NSMakeRange(6, 2)];

NSMutableAttributedString *obliquenessText = [[NSMutableAttributedString alloc] initWithString:@"负值左倾正值右倾"];
[obliquenessText addAttribute:NSObliquenessAttributeName value:@(-0.5) range:NSMakeRange(2, 2)];
[obliquenessText addAttribute:NSObliquenessAttributeName value:@(0.5) range:NSMakeRange(6, 2)];

NSMutableAttributedString *expansionText = [[NSMutableAttributedString alloc] initWithString:@"负值压缩正值拉伸"];
[expansionText addAttribute:NSExpansionAttributeName value:@(-0.5) range:NSMakeRange(0, 4)];
[expansionText addAttribute:NSExpansionAttributeName value:@(0.5) range:NSMakeRange(4, 4)];

NSMutableAttributedString *writingDirectionText = [[NSMutableAttributedString alloc] initWithString:@"文字排版从右往左"];
[writingDirectionText addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)] range:NSMakeRange(0, 8)];

NSMutableAttributedString *attachmentText = [[NSMutableAttributedString alloc] initWithString:@"图文混排"];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"icon_star"];
attachment.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString *attrImage = [NSAttributedString attributedStringWithAttachment: attachment];
[attachmentText appendAttributedString:attrImage];

5.NSAttributedString段落属性:

NSParagraphStyleAttributeName,段落属性(默认是defaultParagraphStyle),对应的值是NSParagraphStyle对象。
NSMutableParagraphStyle属性:

// 行间距
@property CGFloat lineSpacing;
// 段与段之间的间距
@property CGFloat paragraphSpacing;
// 文本两端对齐方式
@property NSTextAlignment alignment;
// 首行缩进
@property CGFloat firstLineHeadIndent;
// 整体缩进(首行除外)
@property CGFloat headIndent;
// 结尾部分的内容超出部分如何显示
@property CGFloat tailIndent;
// 断行方式
@property NSLineBreakMode lineBreakMode;
// 最低行高
@property CGFloat minimumLineHeight;
// 最大行高
@property CGFloat maximumLineHeight;
// 书写方向
@property NSWritingDirection baseWritingDirection;
// 可变行高
@property CGFloat lineHeightMultiple;
// 段首行空白空间
@property CGFloat paragraphSpacingBefore;
// 连字属性,iOS支持的值分别为0和1
@property float hyphenationFactor;

示例代码:

// 行间距5
NSMutableAttributedString *lineSpacingText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *lineSpacingParagraphStyle = [[NSMutableParagraphStyle alloc] init];
lineSpacingParagraphStyle.lineSpacing = 5;
[lineSpacingText addAttribute:NSParagraphStyleAttributeName value:lineSpacingParagraphStyle range:NSMakeRange(0, [text length])];

// 首行缩进20,其余行缩进10
NSMutableAttributedString *lineHeadIndentText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *lineHeadIndentParagraphStyle = [[NSMutableParagraphStyle alloc] init];
lineHeadIndentParagraphStyle.firstLineHeadIndent = 20;
lineHeadIndentParagraphStyle.headIndent = 10;
[lineHeadIndentText addAttribute:NSParagraphStyleAttributeName value:lineHeadIndentParagraphStyle range:NSMakeRange(0, [text length])];

// 段落居中显示
NSMutableAttributedString *alignmentText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *alignmentParagraphStyle = [[NSMutableParagraphStyle alloc] init];
alignmentParagraphStyle.alignment = NSTextAlignmentCenter;
[alignmentText addAttribute:NSParagraphStyleAttributeName value:alignmentParagraphStyle range:NSMakeRange(0, [text length])];

三、实现超链接功能:

先遵守协议UITextViewDelegate

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置超链接富文本
    NSString *str1 = @"大美女";
    NSString *str2 = @"爱上了英俊的";
    NSString *str3 = @"xtayqria";
    NSString *str = [NSString stringWithFormat:@"%@%@%@",str1,str2,str3];
    NSRange range1 = [str rangeOfString:str1];
    NSRange range2 = [str rangeOfString:str2];
    NSRange range3 = [str rangeOfString:str3];
     
    UITextView *textView = [[UITextView alloc] init];
    textView.frame = CGRectMake(40, 100, 300, 50);
    textView.editable = NO;
    textView.delegate = self;
    textView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
    //通过设置UITextView的dataDetectorTypes属性,可以实现识别链接、电话、地址等功能,editable需要设置为NO。
    [self.view addSubview:textView];
    
    NSMutableAttributedString *mastring = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:22.0f]}];
    //设置不同范围的文字颜色
    [mastring addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range1];
    [mastring addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range2];
    [mastring addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:range3];
 
    //设置富文本超链接属性
    // 1.必须要用前缀(firstPerson,secondPerson),随便写但是要有
    // 2.要有后面的方法,如果含有中文,url会无效,所以转码
    NSString *valueString1 = [[NSString stringWithFormat:@"firstPerson://%@", str1] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    NSString *valueString3 = [[NSString stringWithFormat:@"secondPerson://%@", str3] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [mastring addAttribute:NSLinkAttributeName value:valueString1 range:range1];
    [mastring addAttribute:NSLinkAttributeName value:valueString3 range:range3];
 
    //清除超链接本身的颜色
    textView.linkTextAttributes = @{};
    //将你设置的文本信息赋值给textview
    textView.attributedText = mastring;
}

- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
 
    if ([[URL scheme] isEqualToString:@"firstPerson"]) {
        NSString *titleString = [NSString stringWithFormat:@"你点击了第一个文字:%@", [URL host]];
        [self clickLinkTitle:titleString];
        return NO;
    } else if ([[URL scheme] isEqualToString:@"secondPerson"]) {
        NSString *titleString = [NSString stringWithFormat:@"你点击了第二个文字:%@", [URL host]];
        [self clickLinkTitle:titleString];
        return NO;
    }
 
    return YES;
 
}
 
- (void)clickLinkTitle:(NSString *)title {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
 
}

5435345
5435345

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值