富文本(正则表达式)

一.新建工程导入图片

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

_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. linux下淘宝安全控件问题

    2009-09-21  我的环境:ubuntu9.04 firefox3.0.14   下载压缩包http://blog.alipay.com/wp-content/2008/10/aliedit.t ...

  2. iOS 10 的一个重要更新-用 UIViewPropertyAnimator 编写动画

    曾经的黑暗年代 用基于 block 的 UIView animation 来编写 view 属性(frame, transform 等等)变化的动画非常简单.只需要短短几行代码: view.alpha ...

  3. libev ev_io_init学习

    #define ev_io_init(ev,cb,fd,events) / do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } w ...

  4. memcached内存管理机制[未整理]

    memcached默认采用的是Slab Allocator的机制分配管理内存的,在此之前,内存的分配是通过对所有的记录简单地进行malloc和free来进行的,但这种方式容易造成很多内存碎片,加重操作 ...

  5. 富文本编辑器 CKeditor 配置使用

    作者:Tyler Ning出处:http://www.cnblogs.com/tylerdonet/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连 ...

  6. GitHub Desktop 代码库管理工具

    1.GitHub Desktop 简介 GitHub Desktop 是用于 GitHub 项目版本控制软件. 官网下载地址 GitHub Desktop 其它下载地址 GitHub Desktop ...

  7. 我所经历的企业中IT部门在企业内部的地位

    本月参加了一个ITIL的培训,从培训中了解很多关于企业信息化及系统业务运维的知识和方法论.通过这次培训并结合自己近6年的IT 工作经历,明白了以前很多不明白的道理. 先说说自己经历的几家公司吧,我属于 ...

  8. Tensorflow中的run()函数

    1 run()函数存在的意义 run()函数可以让代码变得更加简洁,在搭建神经网络(一)中,经历了数据集准备.前向传播过程设计.损失函数及反向传播过程设计等三个过程,形成计算网络,再通过会话tf.Se ...

  9. python练习笔记——面试题 F(n) = F(n-1)+F(n-2)

    已知:F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) 其中(n≥2,n∈N*) 求:求10以内的函数值分别是多少 方法一: def F(n): if n < ...

  10. Oracle 12C -- clone a non-cdb as a pdb

    将non-CDB置为只读模式: $ sqlplus '/as sysdba' SQL> select name, decode(cdb, 'YES', 'Multitenant Option e ...