简体   繁体   English

使用NSMutableAttributedString在Swift中的UITextView中的粗体文本

[英]Bold part of the text in UITextView in Swift using NSMutableAttributedString

I'm trying to bold part of a text in a UITextView. 我正在尝试在UITextView中加粗文本的一部分。 Here is the code I'm using (though I simplified the text): 这是我正在使用的代码(尽管我简化了文本):

@IBOutlet weak var textview: UITextView!

/*...*/

var str : NSMutableAttributedString = NSMutableAttributedString(string:"this is a test", attributes: [NSFontAttributeName:textview.font!] )
str.addAttribute(NSFontAttributeName, value: UIFont(name: "GillSans-Bold", size: 16)!, range: NSMakeRange(10, 4))
textview.attributedText = str

However it is not working and I don't understand why... All the examples I've seen use this. 但是,它不起作用,我也不明白为什么...我所看到的所有示例都使用了此功能。 There is no error, but nothing in the text is in bold. 没有错误,但文本中没有任何粗体。

Sorry if it's a duplicate, I found plenty of posts talking about putting text in bold in a uitextview, some used this technique but none had problems with it, as far as I know. 抱歉,如果它是重复的,我发现很多帖子都在谈论将文本以粗体显示在uitextview中,有些人使用了这种技术,但据我所知,没有任何问题。 Is there something I missed? 有什么我想念的吗? Did I read something wrong? 我读错了吗?

Thanks in advance. 提前致谢。

One reason might be that the font you're using doesn't exist or is not properly named in the code. 原因之一可能是您使用的字体不存在或在代码中未正确命名。 Here's a neat trick to show the list of all available fonts: 这是一个巧妙的技巧,可以显示所有可用字体的列表:

//credits to Chris: http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/#includefonts
func printFontFamilyNames(){

    for family: String in UIFont.familyNames()
    {
        print("\(family)")
        for names: String in UIFont.fontNamesForFamilyName(family)
        {
            print("== \(names)")
        }
    }

}

This may not directly answer your question but I'm sure you'll find it helpful. 这可能无法直接回答您的问题,但我相信您会发现它会有所帮助。 And also check the link on the top of this code block, from which I've extracted this function. 还要检查此代码块顶部的链接,我从中提取了此功能。

UPDATE 更新

Here's a piece of code I wrote, which properly changes the font of attributed strings within a UILabel : 这是我编写的一段代码,可以正确更改UILabel中属性字符串的字体

if let terms = self.terms {

        let text = NSMutableAttributedString(string: terms.text!)

        let textAsString = text.string as NSString
        let range = NSMakeRange(0, textAsString.length)

        textAsString.enumerateSubstringsInRange(range, options: NSStringEnumerationOptions.ByWords, usingBlock: { (substring, substringRange, enclosingRange, _) -> () in

            if  ["Create", "Account", "Terms", "of", "Service", "Privacy", "policy"].contains(substring!) {

                if let font = UIFont(name: "OpenSans-Semibold", size: 11) {

                    text.addAttribute(NSFontAttributeName, value: font, range: substringRange)

                } else {

                    Services.log("Invalid font")

                }

            }

            //...

        })

        terms.attributedText = text

    }

NOTE: This code contains project specific classes and variables, but I'm sure you can extract what you need. 注意:此代码包含项目特定的类和变量,但是我确定您可以提取所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM