What Is the Way to Save Fonts and Sizes in Firebase for Textview Swift

Can you save attributed strings to Cloud Firestore?

As Doug mentioned in his answer, trying to use NSAttributedString cross platform is challenging as there is no direct Android equivalent so it's probably best to keep the data as primitives.

...But the short answer is: Yes, you can store an NSAttributedString in Firestore because Firestore supports NSData objects.

If you really want to go cross platform with your string style, one thought is to understand that an NSAttributed string is a string with a dictionary of key: value pairs that define the strings look. So you could store primitives in Firestore and then use the appropriate platforms functions to re-assemble the string.

So the string could be stored in Firestore like this

string_0 (a document)
text: "Hello, World"
attrs:
font: "Helvetica"
size: "12"
color: "blue"

You could then read that in as create an attributed string based on those attributes.

That being said, I can get you 1/2 way there on the iOS/macOS side if you really want to store an NSAttributed string in Firestore.

Here's a function that creates an NSAttributedString, archives it and stores the data in Firestore.

func storeAttributedString() {
let quote = "Hello, World"

let font = NSFont.boldSystemFont(ofSize: 20)
let color = NSColor.blue

let intialAttributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: color,
]

let attrString = NSAttributedString(string: quote, attributes: intialAttributes)
let archivedData: Data = try! NSKeyedArchiver.archivedData(withRootObject: attrString, requiringSecureCoding: false)

let dict: [String: Any] = [
"attrString": archivedData
]

let attrStringCollection = self.db.collection("attr_strings")
let doc = attrStringCollection.document("string_0")
doc.setData(dict)
}

then to read it back, here's the function that reads it and displays the attributed string an a macOS NSTextField.

func readAttributedString() {
self.myField.allowsEditingTextAttributes = true //allows rich text
let attrStringCollection = self.db.collection("attr_strings")
let doc = attrStringCollection.document("string_0")
doc.getDocument(completion: { snapshot, error in
if let err = error {
print(err.localizedDescription)
return
}

guard let snap = snapshot else { return }
let archivedData = snap.get("attrString") as! Data
let unarchivedData: NSAttributedString? = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archivedData) as? NSAttributedString
self.myField.attributedStringValue = unarchivedData!
})
}

Swiftui Firebase paragraghs and line breaks in text uploaded to Firestore

With @jnpdx answer this is the solution to the question:

 Text(viewModel.hotspot.info.replacingOccurrences(of: "[br]", with: "\n\n"))

Then go to your articles you upload to Firebase and put "[br]" wherever you want line breaks like below.

Buckingham Fountain is a Chicago Landmark in the center of Grant Park, and between Queen's Landing and Congress Parkway. Dedicated in 1927, it is one of the largest fountains in the world. The crown jewel of Grant Park is fit for a king — literally. The design was inspired by one of the ornate fountains at the Palace of Versailles in France, built for Louis XV. It just happens to be double the size of the original, making it one of the largest fountains in the world. The size is meant to symbolize the enormity of nearby Lake Michigan and uses as much as 15,000 gallons of water per minute. [br]At dusk, Buckingham Fountain comes to life. Head to the park after sunset and you’ll be treated to a spectacular light and music show every hour on the hour. If you’re there during the day, don’t miss the hourly, 20-minute water show featuring the fountain’s center jet shooting water 150 feet into the air. The fountain is active from May through mid-October. Opened to the public in 1927, Buckingham Fountain was commissioned by avid art collector and philanthropist Kate Sturges Buckingham as a memorial for her brother Clarence. [br]The “wedding cake” design was the work of architect Edward H. Bennett, while the fountain’s sculptures — including four sets of Art Deco-style seahorses representing the four states bordering Lake Michigan — were created by French artist Marcel Loyau, who won the Prix National at the 1927 Paris Salon for the project.

UITextView avoid text selection, but keep tappable links

Add this UITextViewDelegate textViewDidChangeSelection and comment out isEditable and isSelectable:

func textViewDidChangeSelection(_ textView: UITextView) {
if textView.selectedTextRange != nil {
textView.delegate = nil
textView.selectedTextRange = nil
textView.delegate = self
}
}

boundingRectWithSize does not respect word wrapping

Did some more research and ended up finding this.

CGSize textSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, FLT_MAX)];
CGFloat textHeight = textSize.height;

Hope this helps someone out there!



Related Topics



Leave a reply



Submit