UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

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

2      

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

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.