Skip to content

Commit

Permalink
Updating readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoatoucher committed Jan 6, 2022
1 parent e13190e commit d0a746b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ You don't have to define custom a `AttributedStringKey` each time you want a cus
<img src="Docs/examples.png" width="400" max-width="80%" alt="glide devices"/>
</p>

### Limitation

Currently there is a bug with `.init(markdown:including:)` initialiser of `AttributedString`. This initializer ignores custom `AttributedStringKey`s used in this library while creating the final string. Due to this, right now it is not possible to style variables and constants of markdown strings, but only localized keys of markdown strings. That is, you have to pass your markdown string as a string literal or from your localization files. Radar reported, a new initialiser will be added once that is fixed.

## Sample usage

```
Expand Down
23 changes: 23 additions & 0 deletions Sources/StyledMarkdown/AttributedString.StyledMarkdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ public extension AttributedString {
)
}

/// Creates an AttributedString with given markdown string and a style group.
/// Due to an iOS bug, watch out for your markdown string to not be a localization key
/// as well which is referencing another string value.
/// - Parameters:
/// - markdown: Markdown string.
/// - styleGroup: Style group to be used to apply styling on the markdown.
init(
markdown: String,
styleGroup: StyleGroup
) {
// Here we are supposed to use `.init(markdown:including:)`
// of AttributedString
// but it has a bug, radar opened and waiting for a fix.
let attributedString = AttributedString(
localized: String.LocalizationValue(markdown),
including: \.styledMarkdown
)
self = Self.annotateStyles(
from: attributedString,
styleGroup: styleGroup
)
}

// MARK: Private functions

private static func annotateStyles(
Expand Down
17 changes: 17 additions & 0 deletions Sources/StyledMarkdown/Text.StyledMarkdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ public extension Text {
self = Text(attributedString)
}

/// Creates a Text with given markdown string and a style group.
/// Due to an iOS bug, watch out for your markdown string to not be a localization key
/// as well which is referencing another string value.
/// - Parameters:
/// - markdown: Markdown string.
/// - styleGroup: Style group to be used to apply styling on the markdown.
init(
markdown: String,
styleGroup: StyleGroup
) {
let attributedString = AttributedString(
markdown: markdown,
styleGroup: styleGroup
)
self = Text(attributedString)
}

}

struct Text_StyledMarkdown_Previews: PreviewProvider {
Expand Down

0 comments on commit d0a746b

Please sign in to comment.