1
votes

I´m still trying to convert TextKit from Objective-C to Swift. It drives me (and you ?) crazy :-( After fixing some problems, now i´m having trouble with the bold (and italic) font:

enter image description here

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
 ...
    var existingTraitsWithNewTrait : UInt32! = nil
    var changedFontDescriptor : UIFontDescriptor! = nil
    if  fontNameAttribute.rangeOfString(traitName).location == NSNotFound {
        existingTraitsWithNewTrait = fontDescriptor.symbolicTraits.rawValue | traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)   
    } else {
        existingTraitsWithNewTrait =  fontDescriptor.symbolicTraits.rawValue & ~traitValue
        changedFontDescriptor = 
            fontDescriptor.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait) // !!!!
   }

In the last line i get the error Cannot convert value of type 'UInt32!' to expected argument type 'UIFontDescriptorSymbolicTraits'

Must i convert UInt32 to UIFontDescriptorSymbolicTraits? The other way is to use .rawValue. But i dont know, how to do this. :-(

Any help is welcome!

1
Please stop adding inappropriate tags. The question is about an issue with Swift code. The question is not about Objective-C in any way.rmaddy
@rmaddy: there do i use Objective-C?Ulli H

1 Answers

2
votes

Try UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait) in place of existingTraitsWithNewTrait in the error line.