swift - 中心和粗体 TextView swift

标签 swift textview

大家好,我正在尝试为我的 TextView 设置粗体和居中, View 中有 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”。您可以在 http://iosfonts.com/ 中查看字体。如果您要将字体添加到项目中,请检查其粗体变体,可能是“Calibri-Bold”

关于swift - 中心和粗体 TextView swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43032604/

相关文章:

ios - 当对象数组为空时显示 UISplitview 辅助 View 的后退屏幕

ios - 将 Floats 数组转换为 UnsafePointer<DSPComplex> 数组

android - 在android中动态生成的TextView下划线

ios - Swift - Textview 识别点击的单词不起作用

css - CSS重现基于磷光体的终端屏幕的效果

java - runinuithread找不到textview

java - 单击时更改 TextView 的背景颜色

swift - 8.3 后无法在 Spotlight 中使用键盘扩展

ios - Swift 是否支持面向方面的编程?

ios - swift 3 : Array Value for key