【发布时间】:2016-01-01 22:48:40
【问题描述】:
我知道已经有人问过类似的问题,但我没有找到解决我的问题的方法。
所以,我有一个 NSString,它有时会在文本中包含一个图像名称,例如:
Lorem ipsum....
image.png
... dolor sit elit lamet
我想在我的 NSMutableAttributedString 中放入一个名为“image.png”的图像。
这是我获取匹配项的代码
NSString *regEx = @"([a-z])+(?:([0-9])+){0,1}.png";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:regEx
options:NSRegularExpressionCaseInsensitive error:&error];
if(error != nil){
NSLog(@"Error: %@",error);
} else{
[regex enumerateMatchesInString:myString options:0 range:NSMakeRange(0, [myString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
// your code to handle matches here
NSLog(@"found!");
}];
}
如何在 NSMutableAttributedString 中添加图像,为每个找到的子字符串调用 imageNamed 函数?
【问题讨论】:
标签: ios objective-c regex nsstring nsmutableattributedstring