码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-07-06 15:09:39      阅读:526      评论:0      收藏:0      [点我收藏+]

标签:正则表达式   ror   expr   append   chm   一个   正则   view   turn   

富文本(正则表达式)

 

一.新建工程导入图片

技术分享

 

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

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

标签:正则表达式   ror   expr   append   chm   一个   正则   view   turn   

原文地址:http://www.cnblogs.com/LiLihongqiang/p/7126224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!