Create a NSButton in code (swift)

Some time ago I shared a simple way to create custom UIButtons in code. As I’m starting to polish NewsWave for Mac, I wanted the same ability to easily create custom buttons. So, I ported the function to macOS, feel free to leverage:       func createNewNSButton(title: String, textSizeDelta: CGFloat?, textColor: NSColor?,  backgroundColor: NSColor?, …

Opening links in Safari / Embedded Safari – iOS

In iOS you can either do it with a push: let safariVC = SFSafariViewController(url: URL(string: “www.google.com”)!, configuration: .init())   present(safariVC, animated: true, completion: nil)   or directly in the Safari app: UIApplication.shared.open(URL(string:“www.google.com”)!, options: [:]) { (_) in }   Very practical,    Marc

Find the right value in an Array, or in an Array of Dictionaries

From time to time I discover a new way of coding something that significantly improves readability & minimizes how much I need to code.  Here’s one of such latest discoveries. Typically to find a value in an array (or in an array of dictionaries) with a specific condition I would do a for loop with …

Inserting cells at the top of a UITableView with no scrolling

If you would like your table to be refreshed “ala Twitter”, with new data being added on top, here’s a quick an easy way to do it for iOS:  func updateTableViewAfterAdditionInNewArray(){         //Get new feeds and check if counts are different         let countOfAddedItems = oldArray.count – newArray.count    …

Random numbers in Swift

Swift 4.2 has introduced a native random number API in the standard library, making it cross platform.  Int.random(in: 1…1000) // → 691 Double.random(in: 0..<1) // → 0.8741555749903935 UInt32.random(in: 0xD800…0xDFFF) // → 55666 Read more in the great article from Ole Begemann, worth a read.    Marc

Resize NSImage Proportionally (Swift)

I wanted a simple function to resize an image to a desired height, while keeping proportions. Here’s what I came up with (based on Marco implementation): extension NSImage {          func resizedImageTo(sourceImage: NSImage, newSize: NSSize) -> NSImage?{         if sourceImage.isValid == false {             …

Open links in the Background

Here’s a quick tip on how to open a URL in the background or foreground depending on a user-set preference.  if(openInBackgroundPreferenceSet == true){                  NSWorkspace.shared.open([linkToOpen], withAppBundleIdentifier: nil, options: NSWorkspace.LaunchOptions.withoutActivation, additionalEventParamDescriptor: nil, launchIdentifiers: nil) }else{                 NSWorkspace.shared.open(linkToOpen) }   Questions / comments? I’m at @MarcMasVi  …