富文本(正则表达式)

一.新建工程导入图片

二 实现方式一(缺点是只能解决一个图片的替换)

_myLabel.font = [UIFont systemFontOfSize:15];

//@"家里真没人 "

//正则表达式 [] 是特殊字符

NSString *str = @"女神: s012 家里真没人 s010 202 s018 ";

//特殊文本 加表情

//    NSAttributedString *

//1.先创建一个值存放文本的字符串并且填充字符串数据

NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:str];

//2.创建一个带图片的附件对象

LHQTextAttachment *attachment = [[LHQTextAttachment alloc]init];

//给附件对象增加一个image

attachment.image = [UIImage imageNamed:@"s014.png"];

//3.在创建一个可以存放待图片文本的

NSAttributedString *strImage = [NSAttributedString attributedStringWithAttachment:attachment];

//4.可变属性字符串拼接普通文本和附件文本

NSMutableAttributedString *mAttributedString = [[NSMutableAttributedString alloc]initWithAttributedString:attributedString];

//    [mAttributedString appendAttributedString:attributedString];

//扫描位置 s012//定一个规则

NSString *pattern = @"[s][0-9]{3}";

NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];

NSTextCheckingResult *result =  [regular firstMatchInString:str options:1 range:NSMakeRange(0, str.length)];

//    result.range =

[mAttributedString replaceCharactersInRange:result.range withAttributedString:strImage];

_myLabel.attributedText = mAttributedString;

实现方式二:

导入分类

#import "NSAttributedString+Emoji.h"

- (void)viewDidLoad {

[super viewDidLoad];

//    _myLabel.font = [UIFont systemFontOfSize:15];

//@"家里真没人 "

//正则表达式 [] 是特殊字符

NSString *str = @"女神: s012 家里真没人 s010 202 s018 ";

NSMutableAttributedString *mAttributedStr = [[NSMutableAttributedString alloc]initWithString:str];

NSAttributedString *attrobutedStr = [NSAttributedString emojiStringWithString:mAttributedStr];

_myLabel.attributedText = attrobutedStr;

}

实现的效果:


附件: 分类的代码

.h:

//

//  NSAttributedString+Emoji.h

//  MiYa

//

//  Created by 李洪强 on 17/07/06.

//  Copyright (c) 2017年 . All rights reserved.

//

#import <Foundation/Foundation.h>

@interface NSAttributedString (Emoji)

+ (NSAttributedString *)emojiStringWithString:(NSMutableAttributedString *)emojiString;

@end

--------------------------------------------------------------------------------------

.m:

//

//  NSAttributedString+Emoji.m

//  MiYa

//

//  Created by 李洪强 on 17/07/06.

//  Copyright (c) 2017年 . All rights reserved.

//

#import "NSAttributedString+Emoji.h"

#import <UIKit/UIKit.h>

@interface EmojiAttachment : NSTextAttachment

@end

@implementation EmojiAttachment

//I want my emoticon has the same size with line's height

- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex

{

return CGRectMake( 0 , -3, lineFrag.size.height, lineFrag.size.height);

}

@end

@implementation NSAttributedString (Emoji)

+ (NSAttributedString *)emojiStringWithString:(NSMutableAttributedString *)emojiString

{

NSRegularExpression *regularEx = [NSRegularExpression regularExpressionWithPattern:@"s[0-9]{3}" options:NSRegularExpressionCaseInsensitive error:nil];

NSString *string = emojiString.string;

NSTextCheckingResult *result = [regularEx firstMatchInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

if (result != nil) {

NSString *imageName = [NSString stringWithFormat:@"%@.png", [string substringWithRange:result.range]];

EmojiAttachment *attachment = [[EmojiAttachment alloc] initWithData:nil ofType:nil];

attachment.image = [UIImage imageNamed:imageName];

NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];

[emojiString replaceCharactersInRange:result.range withAttributedString:attrString];

// 递归

[self emojiStringWithString:emojiString];

} else {

return emojiString;

}

return emojiString;

}

@end

李洪强  2017 07 06于北京

iOS正则表达式的使用案例-富文本的更多相关文章

  1. iOS - UILabel添加图片之富文本的简单应用

    //创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...

  2. iOS开发--使用NSMutableAttributedString 实现富文本

    在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...

  3. [iOS] 利用 NSAttributedString 进行富文本处理

    /iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...

  4. iOS - NSMutableAttributedString富文本的实现

    NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...

  5. 【改】iOS学习之NSAttributedString(富文本)

    NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...

  6. iOS富文本

    背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...

  7. iOS - 富文本AttributedString

    最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢?         ...

  8. iOS之富文本

    之前做项目时遇到一个问题: 使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结果在XCode中查 ...

  9. 【转】iOS使用NSMutableAttributedString实现富文本

    iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...

随机推荐

  1. C#实现多文件上传,写到文件夹中,获取文件信息以及下载文件和删除文件

    前台:.js //上传附件 function uploadAttachment() { if ($("#Tipbind").attr('checked')) { var ip = ...

  2. ios 中UIViewController的分类

    #import <UIKit/UIKit.h> #define TOPVIEWTAG 0x10000 // 导航栏的图片 @interface UIViewController (Chnb ...

  3. 将 numeric 转换为数据类型 numeric 时出现算术溢出错误

    保存数据时控制台报错: Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 将 numeric 转换为数据类型 numeric 时出 ...

  4. java2小结(草稿)

    Struts2 Servlet 小的Java程序,运行在服务器端,接收和响应从客户端发送过来的请求 流程分析: Servlet生命周期? Servlet配置自动加载?(理解) 1.服务器在启动的时候, ...

  5. mysql 常用命令,连接数据库,查看建表语句,批量导入数据,批量更新数据,连接查询

    1. 1)MySQL 连接本地数据库,从cmd中进入mysql命令编辑器: root root分别为用户名和密码 mysql -uroot -proot 2)MySQL 连接本地数据库,用户名为“ro ...

  6. linux 下查看磁盘IO状态

    from:脚本之家 linux 查看磁盘IO状态操作 作者:佚名 字体:[增加 减小] 来源:互联网 时间:11-15 15:13:44我要评论 Linux系统出现了性能问题,一般我们可以通过top. ...

  7. nginx 中文和英文资料

    http://www.nginx.cn/doc/ http://manual.51yip.com/nginx/ http://tool.oschina.net/apidocs/apidoc?api=n ...

  8. linux下串口工具minicom

    系统环境:ubuntu  14 .04  和ubuntu 16.04 我当时的需要主要是两个,能够看到正常串口输出,并且把串口内容实时输出到文件中 那接下来工作主要是两个:1.安装 2.配置 相信各位 ...

  9. Android--------从一个包中的Avtivity创建另外另外一个包的Context

    Android中有Context的概念,想必大家都知道.Context可以做很多事情,打开activity.发送广播.打开本包下文件夹和数据库.获取classLoader.获取资源等等.如果我们得到了 ...

  10. FluentValidation:C#后端输入验证框架的官方文档解读

    参照 FluentValidation 的官方文档写的例子,方便日后查看和使用. 原文:https://github.com/JeremySkinner/FluentValidation/wiki H ...