only return a font if a style is found
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
69ad6e9fb1
commit
64d9ca3777
@ -262,17 +262,20 @@ extension TypographicalStyle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension TypographicalStyle {
|
extension TypographicalStyle {
|
||||||
public static func style(for fontName: String, size: CGFloat) -> TypographicalStyle {
|
public static func style(for fontName: String, size: CGFloat) -> TypographicalStyle? {
|
||||||
//filter all styles by fontName
|
//filter all styles by fontName
|
||||||
let styles = allCases.filter{$0.fontFace.fontName == fontName }.sorted { lhs, rhs in lhs.pointSize < rhs.pointSize }
|
let styles = allCases.filter{$0.fontFace.fontName == fontName }.sorted { lhs, rhs in lhs.pointSize < rhs.pointSize }
|
||||||
//if there are no styles then return defaultStyle
|
|
||||||
guard styles.count > 0 else { return defaultStyle }
|
//if there are no styles then return nil
|
||||||
styles.forEach{print("\($0.rawValue): \($0.pointSize)")}
|
guard styles.count > 0 else { return nil }
|
||||||
|
|
||||||
//if there is an exact match on a style with this pointSize then return it
|
//if there is an exact match on a style with this pointSize then return it
|
||||||
if let style = styles.first(where: {$0.pointSize == size }) {
|
if let style = styles.first(where: {$0.pointSize == size }) {
|
||||||
return style
|
return style
|
||||||
|
|
||||||
} else if let largerIndex = styles.firstIndex(where: { $0.pointSize > size}) { //find the closet one to pointSize
|
} else if let largerIndex = styles.firstIndex(where: { $0.pointSize > size}) { //find the closet one to pointSize
|
||||||
return styles[max(largerIndex - 1, 0)]
|
return styles[max(largerIndex - 1, 0)]
|
||||||
|
|
||||||
} else { //return the last style
|
} else { //return the last style
|
||||||
return styles.last!
|
return styles.last!
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user