IOS開發微博富文本處理及替換表情


//
//  TTWeiboMiddleware.swift
//  TinyTimes
//
//  Created by Alex on 16/3/21.
//  Copyright © 2016年 Alex. All rights reserved.
//

import UIKit
//以前寫的代碼有缺陷這個對於weibocell的補充
class TTWeiboMiddleware: UITableViewCell {
    static var regex:NSRegularExpression?
    func emojiAttributedString(string:String,font:UIFont)->NSAttributedString{
        let parsedOutput = NSMutableAttributedString(string: string, attributes: [NSFontAttributeName:font]);
        let originalString = String(string);
        let originalNSString  = NSString(string: string);
//        var regex:NSRegularExpression? = nil ;
        do{
            if getRegex() == nil{
                TTWeiboMiddleware.regex = try NSRegularExpression(pattern: "\\[[\\u4e00-\\u9fa5|a-z|A-Z]+\\]", options: NSRegularExpressionOptions.CaseInsensitive);
            }
        }catch let err as NSError{
            print (err);
            return parsedOutput;
        }
        if let regex = getRegex(){
            if let emoji = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("emotionImage", ofType: "plist")!){
                let matches = regex.matchesInString(originalString, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0,parsedOutput.string.characters.count));
                let size = CGSizeMake(font.lineHeight,font.lineHeight);
                if matches.count>0{

                    for  var i = matches.count - 1;i>=0;i-- {
                        let result = matches[i];
                        let range = result.range;
                        let placeholder = originalNSString.substringWithRange(range);
                        //如果本地有對應的表情則解析
                        if let emojiName = emoji.objectForKey(placeholder) as? String{
                                if let image = UIImage(named: emojiName){
                                    UIGraphicsBeginImageContext(size);
                                    image.drawInRect(CGRectMake(0,0,size.width,size.height));
                                    let sizedImage = UIGraphicsGetImageFromCurrentImageContext();
                                    UIGraphicsEndImageContext();
                                    let attachment = NSTextAttachment();
                                    attachment.image = sizedImage;
                                    let rep = NSAttributedString(attachment: attachment);
                                    parsedOutput.replaceCharactersInRange(range, withAttributedString: rep);
                                }
                        }
                    }
                        
                }

            }
        
        }
        return parsedOutput;
    }
    func getRegex()->NSRegularExpression?{
        return TTWeiboMiddleware.regex;
    }
}

 

 let matches = regex.matchesInString(originalString, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0,parsedOutput.string.characters.count));



獲取String的長度,一定要用.characters.count 這樣一個英文字符長度是1,一個中文漢字長度也是1 得到的正則表達式對象,應該從后往前替換. 從前往后替換時候會改變字符串長度,導致匹配到的range與原來不一致 PS 感覺swift里面的正則表達式好難用..

效果圖:

里面沒有替換的是本地沒有...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM