在iOS中以旁遮普语加载文本

时间:2018-04-29 05:28:17

标签: ios swift text multilingual

我在下面提到的表格中收到后端的回复,我想同时在旁遮普语,旁遮普语和英语中显示内容,

  {
            "AK_Index" = "APNE SEVAK KEE AAPE RAKE";
            Ang = 747;
            Author = "Guru Arjan Dev Ji";
            Bani = "<null>";
            "Bani_ID" = "<null>";
            English = "Guru Nanak has met the Supreme Lord God; I am a sacrifice to Your Feet. ||4||1||47||";
            "English_Initials" = gnmptckb;
            Gurmukhi = "guru nwnku imilAw pwrbRhmu qyirAw crxw kau bilhwrw ]4]1]47]";
            "Gurmukhi_Initials" = "gnmpqckb]";
            ID = 32133;
            Kirtan = Kirtan;
            "Kirtan_ID" = 2853;
            Punjabi = "gurU nwnk Awpxy prm pRBU nUM iml ipAw hY Aqy aus dy pYrW qoN GolI jWdw hY[";
            Raag = "Raag Soohee";
            "Teeka_Arth" = "hy pRBU! mYN qyry crnW qoN sdky jWdw hW[ ijs mnu`K nUM gurU nwnk iml ipAw, aus nUM prmwqmw iml ipAw ]4]1]47]";
            "Teeka_Pad_Arth" = "kau = nUM, qoN ]4]1]47]";
        }

附件是截图结果如何。截图来自android。我想在iOS中获得完全相同的结果。

enter image description here 如何在iOS中实现这一点。 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

我创建了一个扩展名来更改某个特定字符串的颜色字体。

extension NSMutableAttributedString {

func setFontColorAndSize(_ textToFind : String,withColor color: UIColor,andFont font :UIFont){
    let range = self.mutableString.range(of: textToFind, options: .caseInsensitive)
    if range.location != NSNotFound {
        setAttributes([NSAttributedStringKey.font : font,NSAttributedStringKey.foregroundColor :color], range: range)
    }
}

// This is how you change 
let resultString = NSMutableAttributedString(string: "hello") // convert any string to mutable attributed 
resultString.setFontColorAndSize("hel", withColor: UIColor.grey, andFont: UIFont.boldSystemFont(ofSize: 17)) // pass the string you want to set particular color and font 
UILabel.attributedText = resultString // than update the label

答案 1 :(得分:0)

在移动端你无能为力。您可以要求服务器发送UTF-8编码数据而不是简单的英文字符串。

English = UTF-8 encoded English string data//data for hello
Gurmukhi =  UTF-8 encoded Punjabi string data //data for ਪਜੌਬਹਗਕਤਚਜ 

现在,您可以创建以该语言显示的字符串: -

 let englishString = String(data: English, encoding: .utf8)
 let punjabiSring = String(data: Gurmukhi, encoding: .utf8)

初始化程序是

init?(data: Data, encoding: String.Encoding)

答案 2 :(得分:0)

最后使用“Gurblipi.ttf”字体工作,内部将英文文本更改为旁遮普语。

let attributedString = NSMutableAttributedString()

for shabadDetail in shabadDetailList {
    attributedString.append(NSMutableAttributedString(string: shabadDetail.gurmukhi, attributes: [NSAttributedStringKey.font : FontHelper.guruLippi(fontsize: textview.font!.pointSize), NSAttributedStringKey.foregroundColor: TextFormatting.gurbaniColor]))
    attributedString.append(NSAttributedString(string: TextFormatting.doubleLineBreak))
}

textview.attributedText = attributedString
textview.textAlignment = .center

本机或后端无需编码。