Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Sample PR To show proposed changes [Not mergeable] #76

Closed
wants to merge 10 commits into from
21 changes: 20 additions & 1 deletion ExampleiOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ class ViewController: UIViewController {
let italicStyle = Style {
$0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
}



let style = StyleGroup(base: Style {
$0.font = UIFont.systemFont(ofSize: self.baseFontSize)
$0.lineSpacing = 2
$0.kerning = Kerning.adobe(-15)
}, [
"font": Style {
$0.color = DynamicColorRepresentable(tagKey: "color", defaultColor:"#000000")
},
"strike": Style{
$0.strikethrough = (NSUnderlineStyle.single,UIColor.black)
},
"a":Style{
$0.linkURL = URLRepresentable.tagAttribute("href")
},
"h3": headerStyle,
"h4": headerStyle,
"h5": headerStyle,
Expand All @@ -69,13 +80,21 @@ class ViewController: UIViewController {
$0.paragraphSpacingBefore = self.baseFontSize / 2
$0.firstLineHeadIndent = self.baseFontSize
$0.headIndent = self.baseFontSize * 1.71
$0.color = DynamicColorRepresentable(tagKey: "color", defaultColor:"#000000")
},
"sup": Style {
$0.font = UIFont.systemFont(ofSize: self.baseFontSize / 1.2)
$0.baselineOffset = Float(self.baseFontSize) / 3.5
}])
do {
self.textView?.attributedText = try bodyHTML.set(style: style)
}catch let error {
if let nserror = error as? NSError {
print(nserror)
}

}

self.textView?.attributedText = bodyHTML.set(style: style)
if #available(iOS 10.0, *) {
self.textView?.adjustsFontForContentSizeCategory = true
}
Expand Down
13 changes: 9 additions & 4 deletions ExampleiOS/file.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<strong>Parler du don d'organe n'est plus tabou. Je me renseigne, j'en discute avec mes proches,... et je décide!</strong>
<p><strong>Parler du don d'organe n'est plus tabou. Je me renseigne, j'en discute avec mes proches,... et je décide!</strong></p>

En Belgique, au début des années 2000, le nombre de donneurs d’organes avait tendance à diminuer et les listes d’attente à augmenter considérablement ayant comme corollaire une augmentation de la mortalité des patients inscrits sur les listes d’attente.
En Belgique, au début des années 2000, le nombre de donneurs d’organes avait tendance à diminuer<p> et les listes d’attente à augmenter considérablement ayant comme corollaire une augmentation de la mortalité des patients inscrits sur les listes d’attente<p>.

Soucieux de cette situation, en juin 2005, le Ministre en charge de la Santé publique a souhaité mettre sur pied une vaste campagne de sensibilisation entièrement dédiée au don d’organes.

Expand All @@ -9,7 +9,12 @@ Depuis, de nombreuses actions entreprises par le SPF Santé publique viennent re
<strong>Objectifs poursuivis</strong>

Les objectifs principaux de cette campagne sont:
<li style="text-align: justify;">• d’améliorer la sensibilisation au don auprès des différents groupes auxquels les messages s’adressent,</li>
<li style="text-align: justify;">• d’inviter les citoyens à «oser» en parler en famille, entre amis, entre collègues. Aborder la mort – sa propre mort – reste relativement tabou pour beaucoup.</li>
<li style="text-align: justify;",color="#25883d">• d’améliorer la sensibilisation au don auprès des différents groupes auxquels les messages s’adressent,</li>
<li style="text-align: justify;",color="#25883d">• d’inviter les citoyens à «oser» en parler en famille, entre amis, entre collègues. Aborder la mort – sa propre mort – reste relativement tabou pour beaucoup.</li>


<font color='#ff0000'>₹ 149 has <strike>been refunded back</strike> to your Paypal account.</font>
<font color=#0F1938>&#8377; 149.0</font>


<strong>Pour en savoir plus... <a href="http://www.health.belgium.be/fr/sante/prenez-soin-de-vous/debut-et-fin-de-vie/don-dorganes" target="_blank">www.health.belgium.be/fr/sante/prenez-soin-de-vous/debut-et-fin-de-vie/don-dorganes</a></strong>
25 changes: 24 additions & 1 deletion Sources/SwiftRichString/Attributes/ColorConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,34 @@ import UIKit
/// as color's attributes for several properties inside a style. Style get the color instance from your object
/// and use it inside for string attributes.
/// Both `String` and `UIColor`/`NSColor` already conforms this protocol.
public protocol ColorConvertible {
public protocol ColorConvertible {
/// Transform a instance of a `ColorConvertible` conform object to a valid `UIColor`/`NSColor`.
var color: Color { get }
}


public struct DynamicColorRepresentable:ColorConvertible,DynamicTagComposable{
public var color: Color {
return defaultColor.color
}
public var tagKey: String

// Default color property
public var defaultColor: String

public init(tagKey:String,defaultColor:String) {
self.tagKey = tagKey
self.defaultColor = defaultColor
}

public func dynamicValueFromTag(_ tag: StyleGroup.TagAttribute?) -> Any? {
guard let value = tag?.parameters?[tagKey] else {
return nil
}
return value.color
}
}

// MARK: - ColorConvertible for `UIColor`/`NSColor`

extension Color: ColorConvertible {
Expand Down
32 changes: 16 additions & 16 deletions Sources/SwiftRichString/Extensions/AttributedString+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func add(style: String, range: NSRange? = nil) -> AttributedString {
func add(style: String, range: NSRange? = nil) throws -> AttributedString {
guard let style = Styles[style] else { return self }
return style.add(to: self, range: range)
return try style.add(to: self, range: range)
}

/// Append ordered sequence of styles registered in `StyleManager` to the receiver.
Expand All @@ -61,9 +61,9 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func add(styles: [String], range: NSRange? = nil) -> AttributedString {
func add(styles: [String], range: NSRange? = nil) throws -> AttributedString {
guard let styles = Styles[styles] else { return self }
return styles.mergeStyle().set(to: self, range: range)
return try styles.mergeStyle().set(to: self, range: range)
}

/// Replace any existing attributed string's style into the receiver/substring of the receiver
Expand All @@ -74,9 +74,9 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func set(style: String, range: NSRange? = nil) -> AttributedString {
func set(style: String, range: NSRange? = nil) throws-> AttributedString {
guard let style = Styles[style] else { return self }
return style.set(to: self, range: range)
return try style.set(to: self, range: range)
}

/// Replace any existing attributed string's style into the receiver/substring of the receiver
Expand All @@ -88,9 +88,9 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func set(styles: [String], range: NSRange? = nil) -> AttributedString {
func set(styles: [String], range: NSRange? = nil) throws -> AttributedString {
guard let styles = Styles[styles] else { return self }
return styles.mergeStyle().set(to: self, range: range)
return try styles.mergeStyle().set(to: self, range: range)
}

/// Append passed style to the receiver.
Expand All @@ -100,8 +100,8 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func add(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
return style.add(to: self, range: range)
func add(style: StyleProtocol, range: NSRange? = nil) throws -> AttributedString {
return try style.add(to: self, range: range)
}

/// Append passed sequences of styles to the receiver.
Expand All @@ -112,8 +112,8 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func add(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
return styles.mergeStyle().add(to: self, range: range)
func add(styles: [StyleProtocol], range: NSRange? = nil) throws -> AttributedString {
return try styles.mergeStyle().add(to: self, range: range)
}

/// Replace the attributes of the string with passed style.
Expand All @@ -123,8 +123,8 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func set(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
return style.set(to: self, range: range)
func set(style: StyleProtocol, range: NSRange? = nil) throws -> AttributedString {
return try style.set(to: self, range: range)
}

/// Replace the attributes of the string with a style which is an ordered merge of passed
Expand All @@ -135,8 +135,8 @@ public extension AttributedString {
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: same instance of the receiver with - eventually - modified attributes.
@discardableResult
func set(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
return styles.mergeStyle().set(to: self, range: range)
func set(styles: [StyleProtocol], range: NSRange? = nil) throws -> AttributedString {
return try styles.mergeStyle().set(to: self, range: range)
}

/// Remove passed attribute's keys from the receiver.
Expand Down
20 changes: 10 additions & 10 deletions Sources/SwiftRichString/Extensions/String+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public extension String {
/// - style: name of style registered in `StylesManager` singleton.
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: rendered attributed string, `nil` if style is not registered.
func set(style: String, range: NSRange? = nil) -> AttributedString? {
return StylesManager.shared[style]?.set(to: self, range: range)
func set(style: String, range: NSRange? = nil) throws -> AttributedString? {
return try StylesManager.shared[style]?.set(to: self, range: range)
}

/// Apply a sequence of styles defied in global `StylesManager` to the receiver string.
Expand All @@ -63,8 +63,8 @@ public extension String {
/// - styles: ordered list of styles name to apply. Styles must be registed in `StylesManager`.
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: attributed string, `nil` if all specified styles required are not registered.
func set(styles: [String], range: NSRange? = nil) -> AttributedString? {
return StylesManager.shared[styles]?.mergeStyle().set(to: self, range: range)
func set(styles: [String], range: NSRange? = nil) throws -> AttributedString? {
return try StylesManager.shared[styles]?.mergeStyle().set(to: self, range: range)
}

/// Apply passed style to the receiver string.
Expand All @@ -73,8 +73,8 @@ public extension String {
/// - style: style to apply.
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: rendered attributed string.
func set(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
return style.set(to: self, range: range)
func set(style: StyleProtocol, range: NSRange? = nil) throws -> AttributedString {
return try style.set(to: self, range: range)
}

/// Apply passed sequence of `StyleProtocol` instances to the receiver.
Expand All @@ -86,8 +86,8 @@ public extension String {
/// - styles: ordered list of styles to apply. Styles must be registed in `StylesManager`.
/// - range: range of substring where style is applied, `nil` to use the entire string.
/// - Returns: attributed string.
func set(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
return styles.mergeStyle().set(to: self, range: range)
func set(styles: [StyleProtocol], range: NSRange? = nil) throws -> AttributedString {
return try styles.mergeStyle().set(to: self, range: range)
}

}
Expand All @@ -100,7 +100,7 @@ public extension String {
/// - lhs: plain string.
/// - rhs: style to apply.
/// - Returns: rendered attributed string instance
public func + (lhs: String, rhs: StyleProtocol) -> AttributedString {
return rhs.set(to: lhs, range: nil)
public func + (lhs: String, rhs: StyleProtocol) throws -> AttributedString {
return try rhs.set(to: lhs, range: nil)
}

6 changes: 3 additions & 3 deletions Sources/SwiftRichString/Extensions/UIKit+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension UILabel {
set {
guard let text = newValue else { return }
let style = self.style ?? Style()
self.attributedText = style.set(to: text, range: nil)
self.attributedText = try? style.set(to: text, range: nil)
}
}

Expand Down Expand Up @@ -117,7 +117,7 @@ extension UITextField {
set {
guard let text = newValue else { return }
let style = self.style ?? Style()
self.attributedText = style.set(to: text, range: nil)
self.attributedText = try? style.set(to: text, range: nil)
}
}

Expand Down Expand Up @@ -159,7 +159,7 @@ extension UITextView {
set {
guard let text = newValue else { return }
let style = self.style ?? Style()
self.attributedText = style.set(to: text, range: nil)
self.attributedText = try? style.set(to: text, range: nil)
}
}

Expand Down
20 changes: 17 additions & 3 deletions Sources/SwiftRichString/Style/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,30 @@ public class Style: StyleProtocol {
/// You can pass any `ColorConvertible` conform object, it will be transformed to a valid `UIColor`/`NSColor`
/// automatically. Both `UIColor`/`NSColor` and `String` are conform to this protocol.
public var color: ColorConvertible? {
set { self.set(attribute: newValue?.color, forKey: .foregroundColor) }
set {
if let dynamicColor = newValue as? DynamicTagComposable {
self.set(attribute:dynamicColor, forKey: .foregroundColor)
}
else {
self.set(attribute: newValue?.color, forKey: .foregroundColor)
}
}
get { return self.get(attributeForKey: .foregroundColor) }
}

/// Set the background color of the style.
/// You can pass any `ColorConvertible` conform object, it will be transformed to a valid `UIColor`/`NSColor`
/// automatically. Both `UIColor`/`NSColor` and `String` are conform to this protocol.
public var backColor: ColorConvertible? {
set { self.set(attribute: newValue?.color, forKey: .backgroundColor) }
get { return self.get(attributeForKey: .backgroundColor) }
set {
if let dynamicColor = newValue as? DynamicTagComposable {
self.set(attribute:dynamicColor, forKey: .backgroundColor)
}
else {
self.set(attribute: newValue?.color, forKey: .backgroundColor)
}
}
get { return self.get(attributeForKey: .backgroundColor) }
}

/// This value indicates whether the text is underlined.
Expand Down
Loading