2017-08-05 17 views
1

以前は、カスタムの絵文字を表示するイメージを含む特定の文字列をNSTextAttachmentに変更しました。 NSTextAttachmentコードイメージを含むNSTextAttachmentをStringに置き換えます。

{ 
    guard 
     let original = self.attributedText 
     else { return } 
    let pattern = "\\[img src=(\\w+)\\]" 

    do{ 
     let regex = try NSRegularExpression(pattern: pattern, options: []) 
     let matches = regex.matches(in: original.string, options : [], range : NSMakeRange(0, original.string.characters.count)) 
     let attributeString = NSMutableAttributedString(attributedString: original) 

     for match in matches.reversed(){ 
      let emoticonString = attributeString.attributedSubstring(from: match.rangeAt(1)).string 

      if let emoticonAndroid = Emoticon(rawValue: emoticonString), 
       let image = UIImage(named : "\(emoticonAndroid.convertFromAndroid().rawValue)_000"){ 
       image.accessibilityIdentifier = emoticonAndroid.rawValue 
       let attributedImage = NSTextAttachment() 
       attributedImage.image = image 
       attributedImage.bounds = CGRect(x: 0, y: -8, width: 25, height: 25) 
       attributeString.beginEditing() 
       attributeString.replaceCharacters(in: match.rangeAt(0), with: NSAttributedString(attachment: attributedImage)) 
       attributeString.endEditing() 
      } 
     } 

     self.attributedText = attributeString 
    }catch{ 
     return 
    } 

} 

文字列ではなく、私がメッセージを送信するために、文字列にNSTextAttachmentを交換する必要があります。 NSMutableAttributedString.replaceCharacters(in:with :)メソッドを使用しました。しかし、1つの絵文字イメージでしか動作しません。

one emoticon two emoticons or more

私はそれをどのように修正することができますか?文字コード

{ 
    if let original = self.attributedText{ 
     let attributeString = NSMutableAttributedString(attributedString: original) 

     original.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, original.length), options: [], using: { attribute, range, _ in 
      if let attachment = attribute as? NSTextAttachment, 
       let image = attachment.image{ 
       let str = "[img src=\(image.accessibilityIdentifier!)]" 

       attributeString.beginEditing() 
       attributeString.(in: range, with: str) 
       attributeString.endEditing() 
      } 
     }) 

     self.attributedText = attributeString 
     return attributeString.string 
    }else{ 
     return nil 
    } 
} 

答えて

0

ウムへ

NSTextAttachmentは..私はこの問題を解決しました。

まず:NSTextAttachment

var count = 0 
    self.attributedText.enumerateAttribute(NSAttachmentAttributeName, in : NSMakeRange(0, self.attributedText.length), options: [], using: { attribute, range, _ in 
     if let attachment = attribute as? NSTextAttachment, 
      let image = attachment.image{ 
      count = count + 1 
     } 
    }) 
    return count 

第二の数を数える:文字列でNSTextAttachmentを交換し、変更された範囲を計算します。 < -

for i in 0..<self.countOfNSTextAttachment(){ 
     let attributedString = NSMutableAttributedString(attributedString: self.attributedText) 
     var count = 0 
     attributedString.enumerateAttribute(NSAttachmentAttributeName, in : NSMakeRange(0, attributedString.length), options: [], using: { attribute, range, _ in 
      if let attachment = attribute as? NSTextAttachment, 
       let image = attachment.image{ 
       let str = "[img src=\(image.accessibilityIdentifier!)]" 

       if count == 0{ 
        attributedString.beginEditing() 
        attributedString.replaceCharacters(in: range, with: NSAttributedString(string : str)) 
        attributedString.endEditing() 
        self.attributedText = attributedString 
       }else{ 
        return 
       } 
       count = count + 1 
      } 
     }) 
    } 

    return self.attributedText.string 

結果繰り返しますresult

パーフェクト!

関連する問題