是否有一种unicode方法可以使字符串的一部分变为粗体? [英] Is there a unicode way to make part of a string bold?

查看:270
本文介绍了是否有一种unicode方法可以使字符串的一部分变为粗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Localizable.strings中

in Localizable.strings

"rulesText" = "No illegal posting! \n No weird stuff! \n There is no tolerance for objectionable content, they will be removed!";

我可以参加这个大胆的活动吗?喜欢没有奇怪的东西!或者这个意义上使用unicode字符的东西?或者其他一些方式?

Can I make part of this bold? Like No weird stuff! or something in this sense using unicode characters? Or some other way?

我这样使用它:

textView.text = "\n\n " + NSLocalizedString("rulesText", comment: "")


推荐答案

感谢Martins之前的回答我编辑了他的解决方案以匹配我的案例并且它完美无缺。

签出并提出他的解决方案:创建一个属性字符串在swift for iOS中的普通(Android格式)文本

Thanks to Martins previous answer I edited his solution to match my case and it works perfectly.
Check out and upvote his solution: create an attributed string out of plain (Android formated) text in swift for iOS

所以这基本上改变了:

<a>hey</a> to size 14 bold  
<b>hey</b> to size 12 bold  
<u>hey</u> to underlined  

很容易添加更多功能。

//localizable.strings
"rulesText" = "\n\n<a>The following will be removed</a> \n\n<b><u>Harassment</u></b>\n\nOther Stuff"

//viewdidload
textView.font = UIFont(name: "HelveticaNeue-Light", size: 12) //This is here to set up rest of the texts font
textView.attributedText = convertText(NSLocalizedString("rulesText", comment: "")) 

//method for string conversation
func convertText(inputText: String) -> NSAttributedString {

    var attrString = NSMutableAttributedString(string: inputText)
    let boldFont = UIFont(name: "Helvetica-Bold", size: 12)
    let boldBigFont = UIFont(name: "Helvetica-Bold", size: 14)

    attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldFont!, propsIndicator: "<b>", propsEndIndicator: "</b>")
    attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldBigFont!, propsIndicator: "<a>", propsEndIndicator: "</a>")
    attrString = fixText(attrString, attributeName: NSUnderlineStyleAttributeName, attributeValue: NSUnderlineStyle.StyleDouble.rawValue, propsIndicator: "<u>", propsEndIndicator: "</u>")

    return attrString
}

func fixText(inputText:NSMutableAttributedString, attributeName:AnyObject, attributeValue:AnyObject, propsIndicator:String, propsEndIndicator:String)->NSMutableAttributedString{
    var r1 = (inputText.string as NSString).rangeOfString(propsIndicator)
    while r1.location != NSNotFound {
        let r2 = (inputText.string as NSString).rangeOfString(propsEndIndicator)
        if r2.location != NSNotFound  && r2.location > r1.location {
            let r3 = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length)
            inputText.addAttribute(attributeName as String, value: attributeValue, range: r3)
            inputText.replaceCharactersInRange(r2, withString: "")
            inputText.replaceCharactersInRange(r1, withString: "")
        } else {
            break
        }
        r1 = (inputText.string as NSString).rangeOfString(propsIndicator)
    }
    return inputText
}

这篇关于是否有一种unicode方法可以使字符串的一部分变为粗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆