Swift 4 Attributedstring Get Typing Attributes

Swift 4 attributedString get typing attributes

You can map the [String: Any] dictionary to a
[NSAttributedStringKey: Any] dictionary with

let typingAttributes = Dictionary(uniqueKeysWithValues: self.textView.typingAttributes.map {
key, value in (NSAttributedStringKey(key), value)
})

let text = NSAttributedString(string: "test123", attributes: typingAttributes)

Here is a possible extension method for that purpose, it is
restricted to dictionaries with string keys:

extension Dictionary where Key == String {

func toAttributedStringKeys() -> [NSAttributedStringKey: Value] {
return Dictionary<NSAttributedStringKey, Value>(uniqueKeysWithValues: map {
key, value in (NSAttributedStringKey(key), value)
})
}
}

Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString?

Apple expects you to use enumerateAttributesInRange:options:usingBlock:. The block you supply will receive ranges and the attributes applicable for that range.

I've used that in my code to create invisible buttons that are placed behind text so that it acts as a hyperlink.

You could also use enumerateAttribute:inRange:options:usingBlock: if there's only one you're interested in, but no halfway house is provided where you might be interested in, say, two attributes but not every attribute.

Swift 4 Label attributes

@Larme's comment about the .rawValue not being needed is correct.

Also, you can avoid the force cast that crashes your code using explicit typing:

let strokeTextAttributes: [NSAttributedString.Key: Any] = [
.strokeColor : UIColor.black,
.foregroundColor : UIColor.white,
.strokeWidth : -2.0,
.font : UIFont.boldSystemFont(ofSize: 18)
]

This gets rid of the repetitive NSAttributedString.Key., too.

How to use Attributed String in SwiftUI


iOS 15 and Swift 5.5

Text now supports markdown and also you can create custom attributes:

Sample Image

You can even get defined attributes remotely like:

Sample Image



iOS 13 and 14

You can combine multiple Text objects together with a simple + operator and that will handle some of the attributions:

Sample Image

Each one can have multiple and specific modifiers



A fully supported fallback!

Since it doesn't support directly on Text (till iOS 15), you can bring the UILabel there and modify it in anyway you like:

Implementation:

struct UIKLabel: UIViewRepresentable {

typealias TheUIView = UILabel
fileprivate var configuration = { (view: TheUIView) in }

func makeUIView(context: UIViewRepresentableContext<Self>) -> TheUIView { TheUIView() }
func updateUIView(_ uiView: TheUIView, context: UIViewRepresentableContext<Self>) {
configuration(uiView)
}
}

Usage:

var body: some View {
UIKLabel {
$0.attributedText = NSAttributedString(string: "HelloWorld")
}
}

Instantly format a UITextView using AttributedString

Okay, I just figured out how to use typingAttributtes to solve this question (thanks to @Larme for the hint).

   // Define next attributes
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: NSColor.red,
.font: NSFont(name: "Arial", size: 12)!,
]

// Assign attributes to the text view typing attributes
textView.typingAttributes = attributes

Very easy!



Related Topics



Leave a reply



Submit