Nsfontattributedstring Worked Before Xcode 6.1

NSFontAttributedString worked before XCode 6.1

Xcode 6.1 comes with Swift 1.1 that supports constructors that can fail. UIFont initialisation can fail and return nil. Also use string: when creating NSAttributedString:

if let font = UIFont(name: "Voyage", size: 20.0) {
let timeFont = [NSFontAttributeName:font]
var attrString3 = NSAttributedString(string: "(Time)", attributes : timeFont)
}

NSFontAttributedString worked before XCode 6.1

Xcode 6.1 comes with Swift 1.1 that supports constructors that can fail. UIFont initialisation can fail and return nil. Also use string: when creating NSAttributedString:

if let font = UIFont(name: "Voyage", size: 20.0) {
let timeFont = [NSFontAttributeName:font]
var attrString3 = NSAttributedString(string: "(Time)", attributes : timeFont)
}

Cannot invoke 'subscript' with an argument list of type '(string: NSString, attributes: [NSString : UIFont?])

Unfortunately the error messages from Swift are sometimes not really helpful. The problem is not the subscript, it's the attributes array.

As you can see in the header the UIFont initializer you use returns an optional UIFont:

init?(name fontName: String, size fontSize: CGFloat) -> UIFont

But NSAttributedString initializer expects an [NSObject : AnyObject] array. Note the AnyObject, it's not AnyObject?. So you have to unwrap the UIFont first.

You have two options:

The safe way. Check if those UIFonts could be created, otherwise use system supplied font:

let textboldFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Bold", size: 15.0) ?? UIFont.boldSystemFontOfSize(15.0)]
let textregularFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Regular", size: 15.0) ?? UIFont.systemFontOfSize(15.0)]

The dangerous way. Forcefully unwrap the optional fonts. This will crash if the font could not be created:

let textboldFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Bold", size: 15.0)!]
let textregularFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Regular", size: 15.0)!]

Could not find an overload for “init” that accepts the supplied arguments SWIFT

UIFont(name:size:) is now a failable initializer -- it will return nil if it can't find that font and crash your app if you unwrap the return value. Use this code to safely get the font and use it:

if let font = UIFont(name: "HelveticaNeue-Light", size: 20) {
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.whiteColor()]
}

Class Prefix missing in project template in Xcode 6.1

According to some sources Apple removed this intentionally. They are no longer encouraging the use of prefixes for app code, with the reasoning that app code will not be reused and therefore will have little chance of name conflicts. See: http://scottberrevoets.com/2014/07/25/objective-c-prefixes-a-thing-of-the-past/

They are encouraging it only if you are developing a framework. For frameworks, you can add it using project inspector:
Why is Xcode not prefixing filenames

I use the above method to add prefixes to my app code because I find it neater. Also if some framework I end up using doesn't follow the prefix convention, I can be sure there won't be a name conflict with my code.

Mac development - Start with MacRuby or Objective-C?

Learn Objective-C first. I was in your position and I tried to do MacRuby and didn't get much of anywhere. Now that I've done some Objective-C development, MacRuby is easy, and it's a nice relief to get back to ruby syntax.



Related Topics



Leave a reply



Submit