居中粗体文本视图swift

居中粗体文本视图swift,swift,textview,Swift,Textview,大家好,我正在尝试为我的文本视图设置一个加粗和居中,视图中有3种不同的颜色,但我希望这2种颜色的变化居中。有办法做到这一点吗?下面是我的代码 let myString:String = DisplayableContents.texts[10] var myMutableString = NSMutableAttributedString() myMutableString = NSMutableAttributedString(string:

大家好,我正在尝试为我的文本视图设置一个加粗和居中,视图中有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
试一试

    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

但iOS中不存在“Calibri”。您可以签入字体。如果要将字体添加到项目中,请检查其粗体变体,可能是“Calibri bold”

谢谢您的回复。我已经安装了Calibri字体,包括粗体版本。根据您的答案,它将所有文本输入到文本视图中,我只想将范围内的内容居中。