NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Multiple NSAttributedStrings

Forums > Swift

Hey Everyone, I'm new to Hacking with Swift but not new to swift. I have run into a real issue with NSAttributedStrings. I can make a NSAttributedString and apply it to the part of a string I want. The problem comes when I try to do more. For example say I have a word with an attribute already applied. Now I want to select another word and change it to the same attribute. When I make said selection and apply the attribute I lose the first word. Bottom line I need to be able to change any text with Bold, Italic, Underline, ect multiple times on the fly without a bunch of hard code. If anyone can help that would be great.


I would be interested in a method for doing attributed text (at least a limited RTF implementation). My research so far says SwiftUI doe not support it. SwiftUI supports a limited formatting with inline hard codes attributes (text. italic, etc). 

Ever since SwiftUI came out there have been discussion on this topic around the web. It seems the only viable approach right now is to use UIKit, here is one site:

In my case, I will start by encapsulating all the text editing in my app and make it plain text only for now. Next year formatting (maybe) will come to SwiftUI text and I can replace that module.

BTW: I am also struggling with another issue that seems like a fundamental feature - drag and drop in SwiftUI lists. Sounds easy, just add and edit button and implement an on move. Well, there is no "EditButton" in MacOS, and if you use multiple outline lists in a NavigationView, I don't understand how to use SwiftUI to allow the drag and drop between the lists.

Second, I can't understand how to implement dragging a file from the desktop or another app into a list. So, I am using a using Swift approach and not SwiftUI for that. 

SwiftUI is an outstanding step forward. But, it is having some birthing pains for anyone that wants to use it for a full blown MacOS app with CoreData and iCloud based cooperative editing of attributed text. 

I appreciate any suggestions on how to implement any of those features


Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.