MyLocations v5.0 Hud View page 124

Having trouble creating the HudView. I’m in HudView swift and am using the dictionary to set the font size and color of the Hud text. I’m getting an error " Use of unresolved identifiers ‘NSFontAttributeName’ and ‘NSForegroundColorAttributeName’ ".

I’ve seen this error before when I’ve forgotten to import UIKit or something like that, but I can’t see anywhere in the instructions about importing an NSObject or NS something else. What’s going on here? Should I import something or has Swift changed the identifiers it uses?

Heres my code:

Blockquote

override func draw(_ rect: CGRect) {
let boxWidth: CGFloat = 96
let boxHeight: CGFloat = 96

let boxRect = CGRect(x: round((bounds.size.width - boxWidth) / 2), y: round((bounds.size.height - boxHeight) / 2 ), width: boxWidth, height: boxHeight)

let roundedRect = UIBezierPath(roundedRect: boxRect, cornerRadius: 10)
    UIColor(white: 0.3, alpha: 0.8).setFill()
    roundedRect.fill()
    
    if let image = UIImage(named: "Checkmark") {
        let imagePoint = CGPoint( x: center.x - round(image.size.width / 2), y: center.y - round(image.size.height / 2) - boxHeight / 8)
        
        image.draw(at: imagePoint)
    }
    
    // Create a UIFont object, choose a color for the text, and place it into a dictionary
    
    let attribs = [ NSFontAttributeName: UIFont.systemFont(ofSize: 16), NSForegroundColorAttributeName: UIColor.white ]
    
    let textSize = text.size(attributes: attribs)
    
    let textPoint = CGPoint( x: center.x - round(textSize.width / 2), y: center.y - round(textSize.height / 2) + boxHeight / 4)
    
    text.draw(at: textPoint, withAttributes: attribs)
}

}

You are using the 5th edition of the book, which was written for Xcode 8.x and Swift 3.x. What is your Xcode version? Are you by any chance using Xcode 9.x? If so, you should really be using the 6th edition of the book since there are changes to Swift every year …

This is what it looks like in the latest version:

let attribs = [ NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16),
                NSAttributedStringKey.foregroundColor: UIColor.white ]
1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.