ios - replace specific text to the image using NSMutableAttributedString and regex -
i want replace "[img src=(image name)]" text (image name) image. text replaced blank. not image. should do?
guard let original = self.text else { return } let pattern = "\\[img src=(\\w+)\\]" do{ let regex = try nsregularexpression(pattern: pattern, options: []) let matches = regex.matches(in: original, options : [], range : nsmakerange(0, original.characters.count)) let attributestring = nsmutableattributedstring(string : original) match in matches.reversed(){ let emoticon = attributestring.attributedsubstring(from: match.rangeat(1)).string if let image = uiimage(named : "\(emoticon)"){ let attributedimage = nstextattachment() attributedimage.image = image attributedimage.bounds = cgrect(x : 0, y : 0, width : image.size.width, height : image.size.height) attributestring.replacecharacters(in: match.rangeat(0), with: nsattributedstring(attachment: attributedimage)) } } self.attributedtext = attributestring }catch{ }
ou can't this. neither in swift nor objective-c.
the thing store data want retrieve. is... store name somewhere , use load image. not other way around.
so create property imagename , use image name.
Comments
Post a Comment