Skip to content

Commit

Permalink
Merge branch 'release/2.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Sep 3, 2018
2 parents a08773f + 8c02d45 commit 791ae49
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 96 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# macOS
.DS_Store
/SwiftRichString/SwiftRichString.xcodeproj/xcuserdata/danielemargutti.xcuserdatad
/SwiftRichString/SwiftRichString.xcodeproj/project.xcworkspace/xcuserdata/danielemargutti.xcuserdatad
/SwiftRichString.xcworkspace/xcuserdata/danielemargutti.xcuserdatad

# Xcode
xcuserdata
*.xcuserstate
*.xctimeline
2 changes: 1 addition & 1 deletion Configs/SwiftRichString.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>2.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
14 changes: 0 additions & 14 deletions ExampleiOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

let bold = Style {
$0.font = SystemFonts.Helvetica_Bold.font(size: 20)
}
let normal = Style {
$0.font = SystemFonts.Helvetica_Light.font(size: 15)
}
let red = normal.byAdding {
$0.color = UIColor.red
$0.traitVariants = [TraitVariant.bold,TraitVariant.tightLineSpacing]
}

Styles.register("MyStyle", style: StyleGroup(base: normal, ["bold" : bold, "red" : red]))

return true
}

Expand Down
2 changes: 0 additions & 2 deletions ExampleiOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.label?.styledText = "Hello <red>MERDA</red>"
}

override func didReceiveMemoryWarning() {
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Other info:

### Versions

- **SwiftRichString 2.x branch (current)**. The latest version is [2.0.1](https://github.com/malcommac/SwiftRichString/releases/tag/2.0.1).
- **SwiftRichString 2.x branch (current)**. The latest version is [2.0.2](https://github.com/malcommac/SwiftRichString/releases/tag/2.0.2).
- **SwiftRichString 1.x branch (supported)**. Use [1.1.0 tag](https://github.com/malcommac/SwiftRichString/releases/tag/1.1.0). Its compatible with Swift 4.x.
- **Swift 3.x (no longer mantained)**. Use [0.9.1 release](https://github.com/malcommac/SwiftRichString/releases/tag/0.9.10).

Expand Down Expand Up @@ -487,6 +487,7 @@ The following properties are available:
| font | `FontConvertible` | font used in text |
| color | `ColorConvertible` | foreground color of the text |
| backColor | `ColorConvertible` | background color of the text |
| shadow | `NSShadow` | shadow effect of the text |
| underline | `(NSUnderlineStyle?,ColorConvertible?)` | underline style and color (if color is nil foreground is used) |
| strikethrough | `(NSUnderlineStyle?,ColorConvertible?)` | strikethrough style and color (if color is nil foreground is used) |
| baselineOffset | `Float` | character’s offset from the baseline, in point |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ internal let WATCHOS_SYSTEMFONT_SIZE: CGFloat = 12.0
/// User don't interact with this object directly but via `Style`'s properties.
/// Using the `attributes` property this object return a valid instance of the attributes to describe
/// required behaviour.
internal struct FontInfo {
public class FontData {

private static var DefaultFont = Font.systemFont(ofSize: 12.0)

/// Font object
var font: FontConvertible { didSet { self.style?.invalidateCache() } }
var font: FontConvertible? { didSet { self.style?.invalidateCache() } }

/// Size of the font
var size: CGFloat { didSet { self.style?.invalidateCache() } }
var size: CGFloat? { didSet { self.style?.invalidateCache() } }

#if os(OSX) || os(iOS) || os(tvOS)

Expand Down Expand Up @@ -75,7 +77,9 @@ internal struct FontInfo {

/// Initialize a new `FontInfo` instance with system font with system font size.
init() {
#if os(tvOS)
self.font = nil
self.size = nil
/*#if os(tvOS)
self.font = Font.systemFont(ofSize: TVOS_SYSTEMFONT_SIZE)
self.size = TVOS_SYSTEMFONT_SIZE
#elseif os(watchOS)
Expand All @@ -84,18 +88,63 @@ internal struct FontInfo {
#else
self.font = Font.systemFont(ofSize: Font.systemFontSize)
self.size = Font.systemFontSize
#endif
#endif*/
}

/// Has font explicit value for font name or size
var explicitFont: Bool {
return (self.font != nil || self.size != nil)
}

/// Return a font with all attributes set.
///
/// - Parameter size: ignored. It will be overriden by `fontSize` property.
/// - Returns: instance of the font
var attributes: [NSAttributedStringKey:Any] {
var finalAttributes: [NSAttributedStringKey:Any] = [:]
guard !self.explicitFont else {
return [:]
}
return attributes(currentFont: self.font, size: self.size)
}

/// Apply font attributes to the selected range.
/// It's used to support ineriths from current font of an attributed string.
/// Note: this method does nothing if a fixed font is set because the entire font attributes are replaced
/// by default's `.attributes` of the Style.
///
/// - Parameters:
/// - source: source of the attributed string.
/// - range: range of application, `nil` means the entire string.
internal func addAttributes(to source: AttributedString, range: NSRange?) {
// This method does nothing if a fixed value for font attributes is set.
// This becuause font attributes will be set along with the remaining attributes from `.attributes` dictionary.
guard self.explicitFont else {
return
}

/// Enumerate fonts in string and attach the attributes
let scanRange = (range ?? NSMakeRange(0, source.length))
source.enumerateAttribute(.font, in: scanRange, options: []) { (fontValue, fontRange, shouldStop) in
let currentFont = ((fontValue ?? FontData.DefaultFont) as? FontConvertible)
let currentSize = (fontValue as? Font)?.pointSize
let fontAttributes = self.attributes(currentFont: currentFont, size: currentSize)
source.addAttributes(fontAttributes, range: fontRange)
}
}

/// Return the attributes by sending an already set font/size.
/// If no fixed font/size is already set on self the current font/size is used instead, along with the additional font attributes.
///
/// - Parameters:
/// - currentFont: current font.
/// - currentSize: current font size.
/// - Returns: attributes
public func attributes(currentFont: FontConvertible?, size currentSize: CGFloat?) -> [NSAttributedStringKey:Any] {
var finalAttributes: [NSAttributedStringKey:Any] = [:]

// generate an initial font from passed FontConvertible instance
var finalFont = self.font.font(size: self.size)
guard let size = (self.size ?? currentSize) else { return [:] }
guard var finalFont = (self.font ?? currentFont)?.font(size: size) else { return [:] }

// compose the attributes
#if os(iOS) || os(tvOS) || os(OSX)
Expand All @@ -113,7 +162,6 @@ internal struct FontInfo {

finalFont = finalFont.withAttributes(attributes)


if let traitVariants = self.traitVariants { // manage emphasis
let descriptor = finalFont.fontDescriptor
let existingTraits = descriptor.symbolicTraits
Expand Down
11 changes: 11 additions & 0 deletions Sources/SwiftRichString/Extensions/UIKit+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,14 @@ extension UITextView {
}

#endif

//MARK: - compactMap for Swift 4.0 (not necessary > 4.0)

#if swift(>=4.1)
#else
extension Collection {
func compactMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult] {
return try flatMap(transform)
}
}
#endif
Loading

0 comments on commit 791ae49

Please sign in to comment.