swift2textkituifontdescriptor

TextKit: can´t change Bold Font to "UnBold" in Swift


I have a TextView with some text, which font is Bold. And i´ve a button. When this button is tapped, the Bold Font should change to "UnBold"

enter image description here

There´s a ViewController & a class called "FontManager" to manage the Fonts of the TextKit/UITextView.

My code for the FontManager:

import UIKit

class FontManager {
    var textView: UITextView! = nil

    init(textView: UITextView) {
        self.textView = textView
        self.setFont()
    }

    // set a Range of the TextView to Bold at start:
    func setFont() {
        // set font to "HelveticaNeue"
        let newFont = UIFont(name:"HelveticaNeue", size: textView.font!.pointSize)
        self.textView.font = newFont
        let range = NSMakeRange(11, 24)
        self.makeBold(range)
   }

   // set attributes
   func getDict(key : String, value: UIFont ) -> [String: AnyObject] {
       return [key : value]
   }

This code to set the Font to Bold in selectedRange works without problem

func makeBold(selectedRange: NSRange) {
    self.addOrRemoveFontTraitWithName("Bold", andValue: UIFontDescriptorSymbolicTraits.TraitBold.rawValue, andRange:selectedRange)
}

func addOrRemoveFontTraitWithName(traitName: String, andValue traitValue: UInt32, andRange selectedRange: NSRange) {
    let currentAttributesDict : NSDictionary! = self.textView.textStorage.attributesAtIndex(selectedRange.location, effectiveRange: nil)
    let currentFont : UIFont = currentAttributesDict .objectForKey(NSFontAttributeName) as! UIFont

    let fontDescriptor : UIFontDescriptor! = currentFont.fontDescriptor()
    let fontNameAttribute : NSString = fontDescriptor.objectForKey(UIFontDescriptorNameAttribute) as! NSString

    var existingTraitsWithNewTrait : UInt32! = nil
    var changedFontDescriptor : UIFontDescriptor! = nil

Here is a check, if there is already bold text. If not (== NSNotFound) the text in selectedRange becomes the Bold "trait"

    if fontNameAttribute.rangeOfString(traitName).location == NSNotFound {
        existingTraitsWithNewTrait = fontDescriptor.symbolicTraits.rawValue | traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)

(1) if the Font in selectedRange is bold (else: != NSNotFound) the Font should be set to normal (UnBold)

   } else {
        existingTraitsWithNewTrait =  fontDescriptor.symbolicTraits.rawValue & ~traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))
    }
    let updatedFont =  UIFont(descriptor:changedFontDescriptor, size: 0.0)
    let dict = self.getDict(NSFontAttributeName, value: updatedFont)
    self.textView.textStorage.beginEditing()
    textView.textStorage.setAttributes(dict, range: selectedRange)
    self.textView.textStorage.endEditing()
}

And now the Code of the ViewController:

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    var fontManger: FontManager! = nil

    // the @IBAction to change the Font
    @IBAction func SwitchFont(sender: AnyObject) {
        let range = NSMakeRange(11, 24)
        fontManger.makeBold(range)
    }

    @IBOutlet weak var textView: UITextView!
    var fontManger: FontManager! = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        // init the FontManager
        fontManger = FontManager(textView: self.textView)
    }

At the first Tap of the button there should be a skip to (1), the else...

But this doesn´t happen. Can see that in debug mode???

Any idea is welcome!

download project


Solution

  • Found it! The Bold-Font is "HelveticaNeue-Bold". The "UnBold"-Font is "HelveticaNeue-Medium" but not "HelveticaNeue" and this difference is almost unvisible!

    See no other way as adding

    var updatedFont =  UIFont(descriptor:changedFontDescriptor, size: 0.0)
    if updatedFont.fontName == "HelveticaNeue-Medium" {
        updatedFont =  UIFont(name:"HelveticaNeue", size: textView.font!.pointSize)!
    }
    

    Or do u see a better solution?