首页 > 代码库 > iOS正则表达式的使用案例-富文本

iOS正则表达式的使用案例-富文本

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } span.s1 { } span.s2 { font: 18.0px Menlo }</style>

富文本(正则表达式)

 

一.新建工程导入图片

技术分享

 

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

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828b } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { color: #539aa4 } span.s2 { color: #000000 } span.s3 { color: #703daa } span.s4 { color: #6122ae } span.s5 { } span.s6 { color: #0435ff } span.s7 { font: 18.0px "PingFang SC" } span.s8 { font: 18.0px Menlo; color: #000000 } span.s9 { font: 18.0px Menlo } span.s10 { color: #3e1e81 } span.s11 { color: #c81b13 } span.s12 { color: #c42275 }</style>

_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;

 

 

 

 

实现方式二: 

 

导入分类

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } span.s1 { color: #822d0f } span.s2 { }</style>

#import "NSAttributedString+Emoji.h"

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } span.s1 { } span.s2 { color: #c42275 } span.s3 { color: #000000 } span.s4 { font: 18.0px "PingFang SC" } span.s5 { color: #6122ae } span.s6 { color: #3e1e81 } span.s7 { color: #294c50 } span.s8 { color: #539aa4 } span.s9 { color: #703daa }</style>

- (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:

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #822d0f } span.s4 { color: #c42275 } span.s5 { color: #6122ae }</style>

//

//  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:

 

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #822d0f } span.s4 { color: #c42275 } span.s5 { color: #6122ae } span.s6 { color: #703daa } span.s7 { color: #3e1e81 } span.s8 { color: #0435ff } span.s9 { color: #000000 } span.s10 { color: #c81b13 } span.s11 { color: #3c828b } span.s12 { color: #1e9421 } span.s13 { font: 18.0px "PingFang SC"; color: #1e9421 } span.s14 { color: #294c50 }</style>

//

//  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正则表达式的使用案例-富文本