中心和粗体TextView swift

时间:2017-03-26 18:06:49

标签: swift textview

大家好我想设置一个粗体和中心到我的文本视图我在视图中有3种不同的颜色,但我希望改变的2种颜色居中。有没有办法做到这一点?下面是我的代码。

        let myString:String = DisplayableContents.texts[10]
        var myMutableString = NSMutableAttributedString()


        myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Calibri", size: 17.0)!])

        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:74, length:96))
        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.gray, range: NSRange(location:96, length:19))

        // set label Attribute

        infoBox.attributedText = myMutableString

1 个答案:

答案 0 :(得分:0)

尝试

    let myString:String = DisplayableContents.texts[10]
    var myMutableString = NSMutableAttributedString()

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.Center

    myMutableString = NSMutableAttributedString(string: myString, attributes: [NSParagraphStyleAttributeName:paragraphStyle, NSFontAttributeName:UIFont(name: "Calibri", size: 17.0)!])

    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:74, length:96))
    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.gray, range: NSRange(location:96, length:19))

    // set label Attribute

    infoBox.attributedText = myMutableString

但是Calibri'在iOS中不存在。您可以在http://iosfonts.com/中查看字体。如果要将字体添加到项目中,请检查其粗体变体,可能是Calibri-Bold'