NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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: http://www.gfrigerio.com/nsattributedstring-in-swiftui/

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

   

It seems you can use NSAttributedString in Swift UI.

See the selected answer on this Stack Post:

https://stackoverflow.com/questions/59531122/how-to-use-attributed-string-in-swiftui

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.