-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
> Create a custom `ViewModifier` (and accompanying `View` extension) that makes a view have a large, blue font suitable for prominent titles in a view.
- Loading branch information
CypherPoet
authored and
CypherPoet
committed
Oct 17, 2019
1 parent
c4c42d8
commit d41dd2e
Showing
4 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
.../Projects/ViewsAndModifiers/ViewsAndModifiers/View Modifiers/Banner Font/BannerFont.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// BannerFont.swift | ||
// ViewsAndModifiers | ||
// | ||
// Created by CypherPoet on 10/16/19. | ||
// ✌️ | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
|
||
struct BannerFont: ViewModifier { | ||
let baseSize: CGFloat | ||
|
||
/// We don't use this, but the declaration here | ||
/// asks the system to update the view when it changes | ||
@Environment(\.sizeCategory) var sizeCategory | ||
|
||
|
||
init(baseSize: CGFloat = 92) { | ||
self.baseSize = baseSize | ||
} | ||
|
||
|
||
func body(content: Content) -> some View { | ||
let scaledSize = UIFontMetrics.default.scaledValue(for: 88) | ||
|
||
return content | ||
.font(.system(size: scaledSize)) | ||
.foregroundColor(.blue) | ||
} | ||
} | ||
|
||
|
||
// We could take this even further by having a separate modifier handle | ||
// dynamic sizing (See: https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-dynamic-type-with-a-custom-font) |
18 changes: 18 additions & 0 deletions
18
...ects/ViewsAndModifiers/ViewsAndModifiers/View Modifiers/Banner Font/View+BannerFont.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// View+BannerFont.swift | ||
// ViewsAndModifiers | ||
// | ||
// Created by CypherPoet on 10/16/19. | ||
// ✌️ | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
|
||
extension View { | ||
|
||
func bannerFont(baseSize: CGFloat = 92) -> some View { | ||
self.modifier(BannerFont(baseSize: baseSize)) | ||
} | ||
} |