【发布时间】:2018-07-25 09:48:39
【问题描述】:
我在UILabel 中有一个字符串,该字符串包含一个或多个表情符号,但只有“:cool:”或“:crazy:”之类的名称。
如何将字符串中的单词替换为 .png ?
func checkString(pString: String) -> String {
var replaced = pString //get the String
var found = ""
var image = UIImage() //Emoji
for i in 1...bib.count { //bib = array(Int:String) with all names of the Emoji
if pString.range(of: bib[i]!) != nil {
found = bib[I]!
//picBib is a array(String:String) with the Emoji name and the path
if let picURL: String = picBib[found] as? String {
image = UIImage(named: picURL)!
}
//cute the ":cool:" out of the String
replaced = pString.replacingOccurrences(of: found, with: "")
let imageView = UIImageView(image: image)
imageView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
}
}
return replaced
}
我认为这样的事情有更好的方法.. 表情符号不在图书馆里。我把它们放在 JSON {";P":";P.png",":)":"colon_).png" ...
也许我必须从 UILabel 切换到 ..?
【问题讨论】:
-
使用
NS(Mutable)AttributedString和NSTextAttachment。找出所有出现的:cool:(和其他),并将它们替换为您对应的图像。
标签: swift string uilabel nsattributedstring emoji