美文网首页
cocos UIlabel ios上无法显示带表情微信昵称

cocos UIlabel ios上无法显示带表情微信昵称

作者: invalid_name | 来源:发表于2018-08-09 20:56 被阅读199次

    问题:
    cocos 客户端UILabel 在ios平台显示玩家微信昵称时,遇到带emoji表情昵称显示乱码,安卓可正常显示。

    解决方式:

    收到服务端推送的玩家信息后,将昵称进行转码(lua例):
    
    function M.utf8_to_unicode(convertStr)
        if type(convertStr)~="string" then
            return convertStr
        end
        local resultStr=""
        local i=1
        local num1=string.byte(convertStr,i)
        
        while num1~=nil do
            local tempVar1,tempVar2
            if num1 >= 0x00 and num1 <= 0x7f then
                tempVar1=num1
                tempVar2=0
            elseif bit.band(num1,0xe0)== 0xc0 then
                local t1 = 0
                local t2 = 0
                t1 = bit.band(num1,bit.rshift(0xff,3))
                i=i+1
                num1=string.byte(convertStr,i)
                t2 = bit.band(num1,bit.rshift(0xff,2))
                tempVar1=bit.bor(t2,bit.lshift(bit.band(t1,bit.rshift(0xff,6)),6))
                tempVar2=bit.rshift(t1,2)
            elseif bit.band(num1,0xf0)== 0xe0 then
                local t1 = 0
                local t2 = 0
                local t3 = 0
                t1 = bit.band(num1,bit.rshift(0xff,3))
                i=i+1
                num1=string.byte(convertStr,i)
                t2 = bit.band(num1,bit.rshift(0xff,2))
                i=i+1
                num1=string.byte(convertStr,i)
                t3 = bit.band(num1,bit.rshift(0xff,2))
                tempVar1=bit.bor(bit.lshift(bit.band(t2,bit.rshift(0xff,6)),6),t3)
                tempVar2=bit.bor(bit.lshift(t1,4),bit.rshift(t2,2))
            end
            resultStr=resultStr..string.format("\\u%02x%02x",tempVar2,tempVar1)
            i=i+1
            num1=string.byte(convertStr,i)
        end
        return resultStr
    end
    

    再将返回的字符串传入OC进行解码:

    //解码
    const char *jsonString = [resultStr UTF8String];   
    NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
    NSString *nickName = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
    
    return nickName
    

    返回的昵称可以在ios设备上正常显示。

    相关文章

      网友评论

          本文标题:cocos UIlabel ios上无法显示带表情微信昵称

          本文链接:https://www.haomeiwen.com/subject/pwzcbftx.html