Dynamic formatting via callbacks
I've added an exciting, powerful new feature to TextFormattingRule
s that lets you do even more formatting on the fly. Here's an example:
TextFormattingRule(key: .underlineStyle) { content, range in
if content.count > 10 { return NSUnderlineStyle.double.rawValue }
else { return NSUnderlineStyle.single.rawValue }
}
TextFormattingRule
has a new init()
option that, instead of accepting a static value, accepts a callback function. Whatever the callback returns will be applied as the value for the given NSAttributedString.Key
.
This allows things like tappable links where the URL
target matches a URL string inside HighlightedTextEditor
. See the source code for the .url
[HighlightRule]
preset:
public extension Sequence where Iterator.Element == HighlightRule {
static var url: [HighlightRule] {
[
HighlightRule(pattern: _urlRegex, formattingRules: [
TextFormattingRule(key: .underlineStyle, value: NSUnderlineStyle.single.rawValue),
TextFormattingRule(key: .link) { urlString, _ in
URL(string: urlString) as Any
}
])
]
}
}
This doesn't just apply to NSAttributedString.Key.link
, though, it works on any property! Go crazy!
Behind the scenes
This release also contains substantial improvements to HighlightedTextEditor's continuous integration test suite. These changes will help ship features faster, with less brittle tests.