Skip to content

Commit

Permalink
Set dynamic font size programmatically to remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
BalestraPatrick committed Nov 12, 2017
1 parent f2bd52b commit c4e828c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 17 deletions.
2 changes: 1 addition & 1 deletion WhatsNew.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'WhatsNew'
s.version = '0.2.1'
s.version = '0.3'
s.summary = 'Showcase new features after an app update similar to Pages, Numbers and Keynote.'

# This description is used to generate tags and improve search results.
Expand Down
29 changes: 29 additions & 0 deletions WhatsNew/Items/WhatsNewItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,32 @@ public enum WhatsNewItem {
case text(title: String, subtitle: String)
case image(title: String, subtitle: String, image: UIImage)
}

@objc public protocol WhatsNewItemCustomizable {
weak var titleLabel: UILabel! { get set }
weak var subtitleLabel: UILabel! { get set }
}

public extension WhatsNewItemCustomizable {

public func setDefaultFonts() {
set(titleFont: nil, subtitleFont: nil)
}

// Default implementation.
public func set(titleFont: UIFont? = nil, subtitleFont: UIFont? = nil) {
if let titleFont = titleFont, let subtitleFont = subtitleFont {
titleLabel.font = titleFont
subtitleLabel.font = subtitleFont
return
}
// Use dynamic font size based on user preference if we're on > iOS 10.
if #available(iOS 10, *) {
titleLabel.font = UIFont.preferredFont(forTextStyle: .headline)
subtitleLabel.font = UIFont.preferredFont(forTextStyle: .body)
} else {
titleLabel.font = UIFont.systemFont(ofSize: 20, weight: .bold)
subtitleLabel.font = UIFont.systemFont(ofSize: 16, weight: .regular)
}
}
}
8 changes: 7 additions & 1 deletion WhatsNew/Items/WhatsNewItemImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@

import UIKit

class WhatsNewItemImageView: UIView, NibLoadable {
class WhatsNewItemImageView: UIView, NibLoadable, WhatsNewItemCustomizable {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!

func set(image: UIImage, title: String, subtitle: String, textColor: UIColor) {
NotificationCenter.default.addObserver(self, selector: #selector(refreshFonts), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
imageView.image = image
titleLabel.text = title
titleLabel.textColor = textColor
subtitleLabel.text = subtitle
subtitleLabel.textColor = textColor
setDefaultFonts()
}

@objc func refreshFonts() {
setDefaultFonts()
}
}
8 changes: 7 additions & 1 deletion WhatsNew/Items/WhatsNewItemTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@

import UIKit

class WhatsNewItemTextView: UIView, NibLoadable {
class WhatsNewItemTextView: UIView, NibLoadable, WhatsNewItemCustomizable {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!

func set(title: String, subtitle: String, textColor: UIColor) {
NotificationCenter.default.addObserver(self, selector: #selector(refreshFonts), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
titleLabel.text = title
titleLabel.textColor = textColor
subtitleLabel.text = subtitle
subtitleLabel.textColor = textColor
setDefaultFonts()
}

@objc func refreshFonts() {
setDefaultFonts()
}
}
6 changes: 3 additions & 3 deletions WhatsNew/Resources/WhatsNew.xib
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="DFT-ZF-fEm">
<rect key="frame" x="0.0" y="0.0" width="375" height="90"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="What's New" textAlignment="center" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fKN-eO-Tbq">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="What's New" textAlignment="center" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fKN-eO-Tbq">
<rect key="frame" x="30" y="25" width="315" height="40"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="26"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
Expand Down Expand Up @@ -64,7 +64,7 @@
<constraints>
<constraint firstAttribute="height" constant="45" id="mOy-4M-bMK"/>
</constraints>
<fontDescription key="fontDescription" style="UICTFontTextStyleCallout"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<state key="normal" title="Continue">
<color key="titleColor" red="0.94509803920000002" green="0.92941176469999998" blue="0.039215686270000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
Expand Down
8 changes: 4 additions & 4 deletions WhatsNew/Resources/WhatsNewItemImageView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
<constraint firstAttribute="width" secondItem="YUp-od-Q3r" secondAttribute="height" multiplier="1:1" id="HFl-ee-01d"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Feature Title" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DHq-c8-hDU">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Feature Title" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DHq-c8-hDU">
<rect key="frame" x="100" y="10" width="245" height="50"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Automatically use your preferred font size specified in the Settings app." textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9Mz-s6-ygM">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Automatically use your preferred font size specified in the Settings app." textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9Mz-s6-ygM">
<rect key="frame" x="30" y="70" width="315" height="59"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
Expand Down
12 changes: 6 additions & 6 deletions WhatsNew/Resources/WhatsNewItemTextView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="123"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" text="Automatically use your preferred font size specified in the Settings app." textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bE0-3G-XlW">
<rect key="frame" x="30" y="40" width="315" height="73"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" text="Automatically use your preferred font size specified in the Settings app." textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bE0-3G-XlW">
<rect key="frame" x="30" y="43.5" width="315" height="69.5"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Dynamic Font Size" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="AnA-4W-WKK">
<rect key="frame" x="30" y="10" width="315" height="20.5"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Dynamic Font Size" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="AnA-4W-WKK">
<rect key="frame" x="30" y="10" width="315" height="24"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
Expand Down
15 changes: 14 additions & 1 deletion WhatsNew/WhatsNewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class WhatsNewViewController: UIViewController {

public override func viewDidLoad() {
super.viewDidLoad()

setUp(with: items)
}

Expand All @@ -88,12 +89,24 @@ public class WhatsNewViewController: UIViewController {
stackView.addArrangedSubview(view)
}

NotificationCenter.default.addObserver(self, selector: #selector(refreshFonts), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)

titleLabel.text = titleText
titleLabel.textColor = titleColor
continueButton.setTitle(buttonText, for: .normal)
continueButton.setTitleColor(buttonTextColor, for: .normal)
continueButton.titleLabel?.font = UIFont.preferredFont(forTextStyle: .callout)
continueButton.backgroundColor = buttonBackgroundColor
refreshFonts()
}

@objc private func refreshFonts() {
if #available(iOS 10, *) {
titleLabel.font = UIFont.preferredFont(forTextStyle: .title1)
continueButton.titleLabel?.font = UIFont.preferredFont(forTextStyle: .callout)
} else {
titleLabel.font = UIFont.systemFont(ofSize: 26, weight: .bold)
continueButton.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
}
}

@IBAction func `continue`() {
Expand Down

0 comments on commit c4e828c

Please sign in to comment.