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开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...
随机推荐
- 【laravel5.*】添加ide_helper.php 助手
1.参照文档:https://github.com/barryvdh/laravel-ide-helper#automatic-phpdoc-generation-for-laravel-facade ...
- shell脚本启动node服务
#!/bin/bash cd /root/dev-web source /etc/profile /usr/local/node-8.11.1/bin/npm i && EGG_SER ...
- uitableview 和UISearchBar 下拉提示结合使用
自定cell的代码 餐厅的实体和餐厅对应控件的frame #import <Foundation/Foundation.h> @class RestaurantFrame; @interf ...
- 关于CASE WHEN的多条件汇总问题
https://bbs.csdn.net/topics/392217817?page=1 问题: --创建测试表 IF EXISTS (SELECT * FROM sys.objects WHERE ...
- SQL之group by
转自:理解group by 先来看下表1,表名为test: 表1 执行如下SQL语句: 1 2 SELECT name FROM test GROUP BY name 你应该很容易知道运行的结果,没错 ...
- NSOperation, NSOperationQueue 原理探析
通过GNUstep的Foundation来尝试探索下NSOperation,NSOperationQueue 示例程序 写一个简单的程序 - (void)viewDidLoad { [super vi ...
- webpack window 使用sass来编译css样式
1.执行安装: npm install sass-loader --save-dev (此处不行的话就换上npm install node-sass) 2.稍微修改一下config,删掉我们先前添加的 ...
- XSS安全处理
Security.class.php文件 <?php class Security { public $filename_bad_chars = array( '../', '<!--', ...
- Linux下安装LAMP(Apache+PHP+MySql)和禅道
1.更新yum源: yum update -y 2.安装Apache+PHP+MySql yum install httpd mysql-devel mysql-server mysql-php ph ...
- Oracle VM VirtualBox CentOS7桥接设置问题解决
我遇到的问题是不能选择桥接网络选项,处理步骤: 1.重装 VirtualBox(安装DockerToolBox带的VirtualBox). 2.下面是存在的缺少驱动问题和解决方法: 注意 :缺少桥接驱 ...