Apple introduced a TextEditor in SwiftUI 2.0 in WWDC 20. This provides a multi-line editing capability that was previously missing.

This article covers the TextEditor on macOS, using Swift 5.3+ and SwiftUI 2.0

Capabilities

At this time, the TextEditor is quite useful but also a bit limited. Let’s see how.

How do you use it? It’s quite simple: bind it to a variable.

TextEditor(text: $mytext)

Here’s Apple’s documentation.

How to style the SwiftUI TextEditor on macOS

You can style the editor quite extensively, wrapping it in a VStack for example: (for more options refer to Apple’s docs and View modifiers)

Which could produce something like this:

Styled SwiftUI TextEditor on macOS BigSur


Here’s a simple method that does three things.

  1. Copies a file from location ‘A’ to ‘B’
  2. Removes it from ‘B’ if it already exists
  3. Creates the necessary folder for ‘B’ if it doesn’t exist

You’ll also notice two handy little extensions and we’ll look at that too.

The two extensions fileExists and pathComponent are nothing but String Extensions. Here’s fileExists

Extension String {
var fileExists: Bool {
return FileManager().fileExists(atPath: self)
}
}

It’s handy especially in applications that may be dealing a lot with files on disks.

You will also notice Config.logger.debug

Well, it’s a static usage of Apple’s…


So, you want to design you macOS app in Swift/SwiftUI, and you’d like to have a dropdown on a toolbar icon, like the one shown below. How do you do it?

An image based dropdown on a macOS app toolbar

Well, in SwiftUI 2.0, this is not hard at all. All you need to do is wrap a Menu in a toolbar — like this:

A few items to note:

  1. You can attach a .toolbar to any view, not just a NavigationView — for example, you might have a different toolbar for your sub-view which you navigate to, from the main NavigationView
  2. The ToolbarItemGroup provides a nice grouping…


MD5 is a one-way hashing algorithm. It can be handy in some cases. This short article is not about where it’s appropriate or not — that’s up to you and you can read plenty of articles and arguments on the web.

This is how you can do it in Xcode 11+ / Swift 5+ in 2020, because some articles can be years old and Swift has been changing rapidly. This works on a macOS app, and I guess it will for iOS too.

This example shows how you do MD5 in Swift as a String extension. For e.g., let s = "dan.the.hax0r@hackerzz.com" and then you can do s.MD5

You can, of course, write it as a separate function taking additional input parameters to pad the input string, and so on, but you get the idea.

Hope that helped!


If you’re a genius like me, you need many days of scouring the web and weeping in the night to learn new things like… Swift and SwiftUI.

In this little tutorial, we’ll learn to:

  1. Create a nice little “image button” that calls a function you’ve passed to it.
  2. And shows the image in 32 x 32 no matter what the size of the image was.
  3. And is dynamically colored with your own custom color.

The end result is shown below. You click the button, and whatever function you passed to the view gets called. …

Swift for the Slow

Simple Swift 5+ / Swiftui 2.0 / macOS tips, utilities, sample code, some of which is used in https://powermanuscript.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store