用 swift 中的图片更改字符串
change string with picture in swift
我在 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 切换到 ..?
未测试,但应该可以解决问题:
let string = "Hello :cool:, no? What a :crazy: day!"
let emojiDict: [String: String] = [":cool:": "cool.png", ":crazy:": "crazy.png"]
let attributedString = NSMutableAttributedString(string: string)
for (anEmojiTag, anEmojiImageName) in emojiDict {
let pattern = NSRegularExpression.escapedPattern(for: anEmojiTag)
let regex = try? NSRegularExpression(pattern: pattern,
options: [])
if let matches = regex?.matches(in: attributedString.string,
options: [],
range: NSRange(location: 0, length: attributedString.string.utf16.count)) {
for aMatch in matches.reversed() {
let attachment = NSTextAttachment()
attachment.image = UIImage(named: anEmojiImageName)
//attachment.bounds = something //if you need to resize your images
let replacement = NSAttributedString(attachment: attachment)
attributedString.replaceCharacters(in: aMatch.range, with: replacement)
}
}
}
myLabel.attributedText = attributedString
想法是什么:
使用NS(Mutable)AttributedString
可以将图像放入文本
使用 NSRegularExpression
找到所有出现的地方
从末尾开始替换值(否则,找到的所有范围都可能被破坏,因为出现的长度和替换的长度将不相同。
编辑:
快写在Objective-C(未勾选)
NSString *string = @"Hello :cool:, no? What a :crazy: day!";
NSDictionary *emojiDict = @{@":cool:": @"cool.png", @":crazy:": @"crazy.png"};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
for (NSString *anEmojiText in emojiDict)
{
NSString *anEmojiImageName = emojiDict[anEmojiText];
NSString *pattern = [NSRegularExpression escapedPatternForString:anEmojiText];
NSError *regexError = nil;
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:®exError];
if (regexError) { /* Error management missing here */ }
NSArray *matches = [regex matchesInString:[attributedString string] options:0 range:NSMakeRange(0, [attributedString length])];
[matches enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSTextCheckingResult * _Nonnull aResult, NSUInteger idx, BOOL * _Nonnull stop) {
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setImage:[UIImage imageNamed:anEmojiImageName]];
//[attachment setBounds:something]; //if you need to resize your images
NSAttributedString *replacement = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString replaceCharactersInRange:[aResult range] withAttributedString:replacement];
}];
}
[myLabel setAttributedText:attributedString];
我在 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 切换到 ..?
未测试,但应该可以解决问题:
let string = "Hello :cool:, no? What a :crazy: day!"
let emojiDict: [String: String] = [":cool:": "cool.png", ":crazy:": "crazy.png"]
let attributedString = NSMutableAttributedString(string: string)
for (anEmojiTag, anEmojiImageName) in emojiDict {
let pattern = NSRegularExpression.escapedPattern(for: anEmojiTag)
let regex = try? NSRegularExpression(pattern: pattern,
options: [])
if let matches = regex?.matches(in: attributedString.string,
options: [],
range: NSRange(location: 0, length: attributedString.string.utf16.count)) {
for aMatch in matches.reversed() {
let attachment = NSTextAttachment()
attachment.image = UIImage(named: anEmojiImageName)
//attachment.bounds = something //if you need to resize your images
let replacement = NSAttributedString(attachment: attachment)
attributedString.replaceCharacters(in: aMatch.range, with: replacement)
}
}
}
myLabel.attributedText = attributedString
想法是什么:
使用NS(Mutable)AttributedString
可以将图像放入文本
使用 NSRegularExpression
找到所有出现的地方
从末尾开始替换值(否则,找到的所有范围都可能被破坏,因为出现的长度和替换的长度将不相同。
编辑: 快写在Objective-C(未勾选)
NSString *string = @"Hello :cool:, no? What a :crazy: day!";
NSDictionary *emojiDict = @{@":cool:": @"cool.png", @":crazy:": @"crazy.png"};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
for (NSString *anEmojiText in emojiDict)
{
NSString *anEmojiImageName = emojiDict[anEmojiText];
NSString *pattern = [NSRegularExpression escapedPatternForString:anEmojiText];
NSError *regexError = nil;
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:®exError];
if (regexError) { /* Error management missing here */ }
NSArray *matches = [regex matchesInString:[attributedString string] options:0 range:NSMakeRange(0, [attributedString length])];
[matches enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSTextCheckingResult * _Nonnull aResult, NSUInteger idx, BOOL * _Nonnull stop) {
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setImage:[UIImage imageNamed:anEmojiImageName]];
//[attachment setBounds:something]; //if you need to resize your images
NSAttributedString *replacement = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString replaceCharactersInRange:[aResult range] withAttributedString:replacement];
}];
}
[myLabel setAttributedText:attributedString];