【问题标题】:NSString find regular expression and replace it with image (NSMutableAttributeString)NSString 查找正则表达式并将其替换为图像(NSMutableAttributeString)
【发布时间】: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


    【解决方案1】:

    显然我解决了它。在 for 中有代码用NSMutableAttributedString 中的图像替换子字符串。此外,我决定将一个相反的for 放在一个类似(但不同)的问题中,以启发先前的答案

    NSMutableAttributedString *formattedText = myString.mutableAttributedString;
    
    // Look for image
    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{
        NSArray *matches = [regex matchesInString:myString
                                          options:kNilOptions
                                            range:NSMakeRange(0, myString.length)];
        for (NSTextCheckingResult *result in [matches reverseObjectEnumerator])
        {
            NSString *insideString = [myString substringWithRange:[result rangeAtIndex:0]];
            UIImage *image = [UIImage imageNamed:insideString];
            NSTextAttachment *imageAttachment = [NSTextAttachment new];
            imageAttachment.image = image;
            NSAttributedString *replacementForTemplate = [NSAttributedString attributedStringWithAttachment:imageAttachment];
            [formattedText replaceCharactersInRange:result.range
                               withAttributedString:replacementForTemplate];
    
            NSLog(@"found! -> %@", insideString);
    
        }
    }
    

    相反,mutableAttributedString 属性属于 NSString 类别。这是它的代码

    - (NSMutableAttributedString *)mutableAttributedString {
        return [[NSMutableAttributedString alloc] initWithString:self];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多