0

I'm using attributed text (NSMutableAttributedString, to be exact) to display text that can be either italicized, bold, or have a hyperlink (sometimes combinations of these come up as well).

When I did this with UILabel it displayed exactly as I wanted it, but the links weren't functional. I switched over to UITextView and the links are fully functional, but the italicized and bold fonts aren't working.

We're pulling in the data from xml files, so I can't just specify certain words to set, but I don't think I would need to since this is working with labels.

Cody Harness
  • 1,116
  • 3
  • 18
  • 37

1 Answers1

0

"but the italicized and bold fonts aren't working"

Not sure what exactly is not working but here is a snippet of code that does change font traits in the attributed text:

   @IBOutlet weak var content: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let fontDescriptor = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleBody)

        let existingTraitsWithNewTrait = fontDescriptor.symbolicTraits.rawValue | UIFontDescriptorSymbolicTraits.TraitBold.rawValue | UIFontDescriptorSymbolicTraits.TraitItalic.rawValue


        let changedFontDescriptor: UIFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))!


        let updatedFont = UIFont(descriptor: changedFontDescriptor, size:0.0)

        let attributes = [NSFontAttributeName: updatedFont]

        let attributedString: NSAttributedString? = NSAttributedString(string: "Hello World!", attributes: attributes)
        content.attributedText = attributedString!
    }
John Difool
  • 5,572
  • 5
  • 45
  • 80
  • I've already done all of that. My problem is that those fonts aren't being displayed in this type of view. When I set the attributed text on a label, it looks right, but has no functionality. When I set the attributed text on a view, it functions right but is missing the specific appearance. – Cody Harness Jun 09 '15 at 22:37