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于北京