Skip to content

Commit

Permalink
Better icon and style fallback and messaging
Browse files Browse the repository at this point in the history
Missing icons will now fall back to “question-circle” available in the FA free set, and an error message will be printed.

If no style is specified and “regular” is not available, the first available style will be used instead, but silently.

If a style is specified but unavailable, same behavior as above, but an error message will be printed to log.
  • Loading branch information
mattmaddux committed Feb 20, 2020
1 parent c42363d commit 2e043ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
6 changes: 1 addition & 5 deletions Sources/FASwiftUI/FASearchHolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import SwiftUI

class FASearchHolder: ObservableObject {

@Published var searchQuery: String = "" {
didSet {
print("Set: \(searchQuery)")
}
}
@Published var searchQuery: String = ""
@Published var searchResults: [String: FAIcon]?

private var keyboardVisible: Bool = false
Expand Down
34 changes: 23 additions & 11 deletions Sources/FASwiftUI/FAText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@ import SwiftUI
public struct FAText: View {

var iconName: String
private var icon: FAIcon {
return FontAwesome.shared.icon(byName: iconName) ?? FontAwesome.shared.icon(byName: "question-square")!
}
private var icon: FAIcon
var size: CGFloat
var style: FAStyle?
var style: FAStyle
private var weight: Font.Weight {
if let style = style {
return style.weight
} else {
return .regular
}
return style.weight
}

public init(iconName: String, size: CGFloat, style: FAStyle? = nil) {
self.iconName = iconName.hasPrefix("fa-") ? String(iconName.dropFirst(3)) : iconName
self.size = size
self.style = style
self.style = style ?? .regular
self.iconName = iconName.hasPrefix("fa-") ? String(iconName.dropFirst(3)) : iconName

if let icon = FontAwesome.shared.icon(byName: self.iconName) {
self.icon = icon
} else {
self.icon = FontAwesome.shared.icon(byName: "question-circle")!
self.style = .regular
print("FASwiftUI: Icon \"\(iconName)\" not found. Check list at https://fontawesome.com/icons for set availability.")
}

if !self.icon.styles.contains(self.style) {
let fallbackStyle = self.icon.styles.first!
if fallbackStyle != .brands && style != nil {
print("FASwiftUI: Style \"\(style ?? .regular)\" not available for icon \"\(iconName)\", using \"\(fallbackStyle)\". Check list at https://fontawesome.com/icons for set availability.")
} else if self.style != .regular && style != nil {
print("FASwiftUI: Icon \"\(iconName)\" is part of the brands set and doesn't support alternate styles. Check list at https://fontawesome.com/icons for set availability.")
}
self.style = fallbackStyle
}
}

public var body: some View {
Expand Down

0 comments on commit 2e043ec

Please sign in to comment.