0

I'm trying to bold part of a text in a 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. Is there something I missed? Did I read something wrong?

Thanks in advance.

Lyra
  • 301
  • 1
  • 4
  • 19
  • If you apply the `GillSans-Bold` in the first line, does it works? – Larme Nov 30 '15 at 17:16
  • Log `str`. Does it show what you expect? – rmaddy Nov 30 '15 at 17:26
  • I can't reproduce the error, it [works for me](https://www.evernote.com/l/AFnpjPXt7tRLapGLwpHqE5TsGiNRUzdoH9Y) in a Playground. – Eric Aya Nov 30 '15 at 17:31
  • Indeed, I just tested it in the playground as well and it works there, but not in my project – Lyra Nov 30 '15 at 18:09
  • Then you can forget/delete this question. If it works, it works: this code has no errors. The fact that it doesn't work in your app means *something else* prevents this to work. You have to debug your app now, to find what and where. :) – Eric Aya Nov 30 '15 at 18:17
  • It's very weird... I tried setting it up in the storyboard by choosing Attributed instead of Plain for the textview, and if I change the color it works and shows in the app, but not when I set a word to bold. I have no idea how to debug that. – Lyra Dec 02 '15 at 11:08

1 Answers1

-1

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:

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.

Armin
  • 629
  • 6
  • 23
  • "GillSans-Bold" is the correct name, this is not OP's error. – Eric Aya Nov 30 '15 at 17:45
  • The font name is correct, I used it for other things :/ (also the code you copied doesn't work for me, it tells me it "can't invoke familyNames with no arguments", but the function indeed requires no argument so I don't understand the problem... is there something I need to do before calling it?) – Lyra Nov 30 '15 at 18:01
  • @Lyra The code example by Armin works, I just tested in a Playground. Are you sure you're using a current version of Xcode? – Eric Aya Nov 30 '15 at 18:03
  • I'm sorry I couldn't have been more helpful, although the downvote is a bit much. I've updated my answer with an example I used, which may give you a better insight. Note that I'm using a label instead of UITextVIew. – Armin Nov 30 '15 at 18:13