Skip to content

Commit

Permalink
Complete Challenge 1
Browse files Browse the repository at this point in the history
> 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
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
F317F7582357C94700B56343 /* BannerFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = F317F7572357C94700B56343 /* BannerFont.swift */; };
F317F75A2357CB2500B56343 /* View+BannerFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = F317F7592357CB2500B56343 /* View+BannerFont.swift */; };
F3CDC3F52356A80500FB7151 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3CDC3F42356A80500FB7151 /* AppDelegate.swift */; };
F3CDC3F72356A80500FB7151 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3CDC3F62356A80500FB7151 /* SceneDelegate.swift */; };
F3CDC3F92356A80500FB7151 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3CDC3F82356A80500FB7151 /* ContentView.swift */; };
Expand All @@ -16,6 +18,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
F317F7572357C94700B56343 /* BannerFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BannerFont.swift; sourceTree = "<group>"; };
F317F7592357CB2500B56343 /* View+BannerFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+BannerFont.swift"; sourceTree = "<group>"; };
F3CDC3F12356A80500FB7151 /* ViewsAndModifiers.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ViewsAndModifiers.app; sourceTree = BUILT_PRODUCTS_DIR; };
F3CDC3F42356A80500FB7151 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
F3CDC3F62356A80500FB7151 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -37,6 +41,23 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
F317F7552357C92100B56343 /* View Modifiers */ = {
isa = PBXGroup;
children = (
F317F7562357C92A00B56343 /* Banner Font */,
);
path = "View Modifiers";
sourceTree = "<group>";
};
F317F7562357C92A00B56343 /* Banner Font */ = {
isa = PBXGroup;
children = (
F317F7572357C94700B56343 /* BannerFont.swift */,
F317F7592357CB2500B56343 /* View+BannerFont.swift */,
);
path = "Banner Font";
sourceTree = "<group>";
};
F3CDC3E82356A80500FB7151 = {
isa = PBXGroup;
children = (
Expand All @@ -56,6 +77,7 @@
F3CDC3F32356A80500FB7151 /* ViewsAndModifiers */ = {
isa = PBXGroup;
children = (
F317F7552357C92100B56343 /* View Modifiers */,
F3CDC3F42356A80500FB7151 /* AppDelegate.swift */,
F3CDC3F62356A80500FB7151 /* SceneDelegate.swift */,
F3CDC3F82356A80500FB7151 /* ContentView.swift */,
Expand Down Expand Up @@ -146,9 +168,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F317F7582357C94700B56343 /* BannerFont.swift in Sources */,
F3CDC3F52356A80500FB7151 /* AppDelegate.swift in Sources */,
F3CDC3F72356A80500FB7151 /* SceneDelegate.swift in Sources */,
F3CDC3F92356A80500FB7151 /* ContentView.swift in Sources */,
F317F75A2357CB2500B56343 /* View+BannerFont.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@

import SwiftUI


struct ContentView: View {

var body: some View {
Text("Hello World")
Text("Hello World!")
.bannerFont()
}
}


struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
Expand Down
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)
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))
}
}

0 comments on commit d41dd2e

Please # to comment.