swift alternative for fontDescriptorWithSymbolicTraits ios 8 bug
<p>I'm trying to create a Swift custom text editor (lite version) and got stuck on iOS 8 fontDescriptorWithSymbolicTraits bug, returning nil. Are there any Swift workarounds?</p>

<p>I've created a <a href="https://www.dropbox.com/s/o9j8v2imkdwqu3r/TextKitBug.zip?dl=1" rel="nofollow noreferrer">small project sample here</a> , you can download and run the following scenario:</p>

<ol>
<li>Select a text</li>
<li>Click on "<strong>B</strong>" button</li>
<li>Click on "<strong>I</strong>" button</li>
</ol>

<p>The app crashes, <strong><code>fontDescriptorWithSymbolicTraits</code></strong> returns <strong><code>nil</code></strong> :(</p>

<pre><code>private func addOrRemoveTraitWithName(traitName: String, traitValue: UInt32) {
    let range = self.textEditor.selectedRange
    let currentAttributesDict = self.textEditor.textStorage.attributesAtIndex(range.location, effectiveRange: nil)
    let currentFont = currentAttributesDict[NSFontAttributeName]

    let fontDescriptor = currentFont?.fontDescriptor()
    let fontNameAttribute = fontDescriptor?.fontAttributes()[UIFontDescriptorNameAttribute]

    var changedFontDescriptor: UIFontDescriptor?

    if fontNameAttribute?.rangeOfString(traitName).location == NSNotFound {
      let existingTraitsWithNewTrait = UIFontDescriptorSymbolicTraits(rawValue: fontDescriptor!.symbolicTraits.rawValue | traitValue)
      changedFontDescriptor = fontDescriptor?.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait)
    } else {

      let existingTraitsWithoutTrait = UIFontDescriptorSymbolicTraits(rawValue: fontDescriptor!.symbolicTraits.rawValue &amp; ~traitValue)
      changedFontDescriptor = fontDescriptor?.fontDescriptorWithSymbolicTraits(existingTraitsWithoutTrait)

    }

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

    var dict = [String : AnyObject]()
    dict[NSFontAttributeName] = updatedFont
    self.textEditor.textStorage.beginEditing()
    self.textEditor.textStorage.setAttributes(dict, range: range)
    self.textEditor.textStorage.endEditing()
}
</code></pre>

<p>I highly recommend to take a quick look at <a href="https://www.dropbox.com/s/o9j8v2imkdwqu3r/TextKitBug.zip?dl=1" rel="nofollow noreferrer">sample project</a></p>

<p>What options do I have? </p>

<p>I also seen that <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIFontDescriptor_Class/#//apple_ref/occ/clm/UIFontDescriptor/fontDescriptorWithFontAttributes:" rel="nofollow noreferrer">on Apple documentation the method</a> that was used by other is missing from the SDK &amp; documentation </p>

<p>What others say about this:</p>

<ol>
<li><a href="https://stackoverflow.com/questions/25977207/font-descriptor-returns-nil-in-ios-8#comment-40782651">Font Descriptor returns nil in iOS 8</a></li>
<li><a href="http://www.openradar.me/19922049" rel="nofollow noreferrer">http://www.openradar.me/19922049</a></li>
</ol>