用swift中的图片更改字符串

问题描述 投票:1回答:1

我在UILabel中有一个字符串,这个字符串包含一个或几个表情符号,但只有像“:cool:”或“:crazy:”这样的名字。

如何用.png替换String中的单词?

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”,“:)”:“冒号_”。png“......

也许我必须从UILabel切换到..?

swift string uilabel nsattributedstring emoji
1个回答
1
投票

没有经过测试,但应该可以解决这个问题:

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:&regexError];
    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];
© www.soinside.com 2019 - 2024. All rights reserved.