iOS正则表达式的使用案例-富文本
富文本(正则表达式)
一.新建工程导入图片

二 实现方式一(缺点是只能解决一个图片的替换)
_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正则表达式的使用案例-富文本的更多相关文章
- iOS - UILabel添加图片之富文本的简单应用
//创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...
- iOS开发--使用NSMutableAttributedString 实现富文本
在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...
- [iOS] 利用 NSAttributedString 进行富文本处理
/iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...
- iOS - NSMutableAttributedString富文本的实现
NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...
- 【改】iOS学习之NSAttributedString(富文本)
NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...
- iOS富文本
背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...
- iOS - 富文本AttributedString
最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢? ...
- iOS之富文本
之前做项目时遇到一个问题: 使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结果在XCode中查 ...
- 【转】iOS使用NSMutableAttributedString实现富文本
iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...
随机推荐
- Docker命令分类及使用场景分布(脑图)
常见的Docker命令分类主要有 不同使用场景下的命令分布 有疑问可到官方文档查询: https://docs.docker.com/engine/reference/commandline/dock ...
- c++ ado 程序终止时崩溃
在_ConnectionPtr析构的时候要将_ConnectionPtr置NULL ADODB::_ConnectionPtr conn;conn.CreateInstance(__uuidof(AD ...
- tensorflow内存溢出问题
Tensorflow的静态图结构简洁清晰,符合人的思维.虽然编程上略微有些复杂,但是原理很容易看懂. Tensorflow分建图过程和运行图(张量求值)两个阶段,在这两个阶段中都可以定义操作和张量.但 ...
- Fisher准则一维聚类
在做FAQ系统时,用户输入一个查询之后,返回若干个打好分数的文档.对于这些文档,有些是应该输出的,有些是不应该输出的.那么应该在什么地方截断呢? 这个问题其实是一个聚类问题,在一维空间中把若干个点聚成 ...
- 【MATLAB】matlabR2010a与vs2010联合编译设置
在matlab中编译C++程序,首先要配置编译器>> mex -setupPlease choose your compiler for building external interfa ...
- mysqlsla快速入门
小强软件测试,因为不是天生丽质,所以必须天生励志. 性能.python自动化班长期招生,咨询QQ:2083503238 官网:http://xqtesting.sxl.cn QQ群:229390571 ...
- java 实现二分法
http://www.cnblogs.com/vanezkw/archive/2012/06/29/2569470.html JDK里面的二分法实现.二分法的实现有多种今天就给大家分享两种.一种是递归 ...
- android安卓系统上运行jar文件
原文链接: http://blog.sina.com.cn/s/blog_658c8cea0101mdhp.html 步骤如下: 1. 将文件打包成可执行jar文件(可在eclipse里export) ...
- sql用逗号连接多张表对应哪个join?
转自:http://blog.csdn.net/huanghanqian/article/details/52847835 四种join的区别已老生常谈: INNER JOIN(也可简写为JOIN): ...
- 手动脱WinUpack 壳实战
作者:Fly2015 吾爱破解培训第一课选修作业第6个练习演示样例程序.不得不反复那句话,没见过这样的壳,该壳是压缩壳的一种,相对于压缩壳,加密壳的难度要大一些.特别是IAT表的修复问题上. 首先分别 ...