Questions tagged [attributedstring]
27 questions
10
votes
1 answer
How to render Markdown headings in SwiftUI AttributedString?
I've been trying to use the new AttributedString that released with iOS 15 to render Markdown stored in a variable. However, I haven't been able to find a way for it to render markdown headings such as:
# Title 1
### Title 3
###### Title 6
Here's…

Clément Cardonnel
- 4,232
- 3
- 29
- 36
5
votes
4 answers
How to change hyper link text color in SwiftUI
I am trying to custom change the default font color of hyperlink in a given markdown string with SwiftUI. Something equivalent to txtString.linkTextAttributes = [ .foregroundColor: UIColor.red ] of UIKit.
Here's my code:
import SwiftUI
struct…

Vijay Lama
- 96
- 1
- 6
3
votes
1 answer
How do you add an image attachment to an AttributedString?
I'm working to replace NSAttributedString with AttributedString but have been unsuccessful in getting attachments to work. The image doesn't appear in the string despite the fact I applied the attachment.
let textAttachment = NSTextAttachment(image:…

Jordan H
- 52,571
- 37
- 201
- 351
2
votes
1 answer
Change just the string of an AttributedString (e.g. UIButton configuration attributedTitle)
Starting in iOS 15, Swift provides the AttributedString struct that embodies a string's text along with its style attributes. Question: given an existing AttributedString, and assuming (for the sake of simplicity) that the attributes consist of a…

matt
- 515,959
- 87
- 875
- 1,141
2
votes
2 answers
Text() is ignoring multiple paragraphs from AttributedString(markdown: ...)
Given the markdown string "**Line 1**\n\nLine 2" I expect an output of
Line 1
Line 2
Instead I get
Line 1Line 2
Surely this isn't a limitation of markdown or AttributedString. What am I missing?! How do I specify multiple paragraphs if not with two…

Rumbles
- 806
- 1
- 8
- 15
2
votes
1 answer
How to detect a tap on a link and tap coordinates at the same time in SwiftUI?
In my SwiftUI app, some parts of text need to be tappable. On taps, some custom actions should occur, not necessarily opening a web page. At the same time I need to detect tap coordinates. I was going to use a drag gesture handler for that.
I…

Alexander Poleschuk
- 892
- 12
- 22
1
vote
1 answer
How to get attribute from AttributeContainer in UIKit and assign it to some variable
I create next subclass:
import UIKit
class Button: UIButton {
override func updateConfiguration() {
var config = configuration ?? UIButton.Configuration.plain()
let color = config.attributedTitle?.foregroundColor
…

Petr Bones
- 11
- 3
1
vote
1 answer
How to chain UIKit attributes of an AttributeContainer?
Starting in iOS 15, you can style text in UIKit with an AttributedString. You can treat the attributes as properties of the AttributedString, or you can make an AttributeContainer and treat the attributes as properties of the AttributeContainer and…

matt
- 515,959
- 87
- 875
- 1,141
1
vote
1 answer
Create attributed string based on cell type in Swift
I am currently developing a Notifications list screen that would display 4 types of notifications. Here is how it would look like in a very simple way:
The main problem for me is to make the text attributed properly depending on the cell type. I…

Stefan
- 35
- 5
1
vote
2 answers
Getting the raw Markdown string from an AttributedString in Swift
Is there a way to get the Markdown string from an AttributedString? I have the following code:
let text = "**Hello** World!"
let attString = AttributedString(text)
Now I want to get back the Markdown string.
I did see that AttributedString includes…

reza23
- 3,079
- 2
- 27
- 42
1
vote
0 answers
SwiftUI: Attributed string from HTML that wraps
I have two strings that can contain HTML strings.
HTML string can only contain simple text formatting like bold, italic etc.
I need to combine these in an attributed string and show in a list.
Every list item may have a leading or a trailing item…

Olcay Ertaş
- 5,987
- 8
- 76
- 112
1
vote
2 answers
How to copy all attributes from one AttributedString to another without converting to NSAttributedString?
I have an AttributedString with a set of attributes I don't know until runtime. Now I want to append / prepend another string. How do I get the appended string to have the same attributes as the original AttributedString? Copying the attributes from…

Nic0S
- 13
- 2
1
vote
2 answers
Swift: AttributedString underline with different color
I'm trying to give different color for underlined text in the AttributedString. as per this example.
But the result with this snippet of code:
var attributedString = try AttributedString(markdown: self)
if let rangeWord =…

Mohamed Emad Hegab
- 2,665
- 6
- 39
- 64
1
vote
1 answer
How to get the size of the new swift AttributedString?
NSAttributedString has a size() method which the new AttributedString lacks. How do you get the size of the new AttributedString?

Melodius
- 2,505
- 3
- 22
- 37
0
votes
3 answers
Using NSFont with AttributeContainer
When creating an AttributedString for use in AppKit I get a warning that NSFont isn't Sendable:
var container = AttributeContainer()
container.appKit.foregroundColor = .red
container.appKit.font = .systemFont(ofSize: mySize)
// ^…

smr
- 890
- 7
- 25