Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UIFontMetrics to scale fonts #64

Merged
merged 1 commit into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Source/TextStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,42 @@ public struct TextStyle: Hashable, Equatable {
public let attributes: [NSAttributedStringKey: Any]
public let minSize: CGFloat
public let maxSize: CGFloat
public let scalingTextStyle: UIFont.TextStyle

public init(
font: Font = .system(.default),
size: CGFloat = UIFont.systemFontSize,
attributes: [NSAttributedStringKey: Any] = [:],
minSize: CGFloat = 0,
maxSize: CGFloat = .greatestFiniteMagnitude
maxSize: CGFloat = .greatestFiniteMagnitude,
scalingTextStyle: UIFont.TextStyle = .body
) {
self.font = font
self.size = size
self.attributes = attributes
self.minSize = minSize
self.maxSize = maxSize
self.scalingTextStyle = scalingTextStyle

self._hashValue = font
.combineHash(with: size)
.combineHash(with: attributes.count)
.combineHash(with: minSize)
.combineHash(with: maxSize)
.combineHash(with: scalingTextStyle)
}

public func font(contentSizeCategory: UIContentSizeCategory) -> UIFont {
let preferredSize = contentSizeCategory.preferredContentSize(size)
let calculatedSize: CGFloat

if #available(iOS 11.0, *) {
let metrics = UIFontMetrics(forTextStyle: scalingTextStyle)
calculatedSize = metrics.scaledValue(for: size, compatibleWith: UITraitCollection(preferredContentSizeCategory: contentSizeCategory))
} else {
calculatedSize = size * contentSizeCategory.approximateScaleFactor(forTextStyle: scalingTextStyle)
}

let preferredSize = min(max(calculatedSize, minSize), maxSize)

switch font {
case .name(let name):
Expand Down Expand Up @@ -72,6 +85,7 @@ public struct TextStyle: Hashable, Equatable {
&& lhs.minSize == rhs.minSize
&& rhs.maxSize == rhs.maxSize
&& lhs.font == rhs.font
&& lhs.scalingTextStyle == rhs.scalingTextStyle
&& NSDictionary(dictionary: lhs.attributes).isEqual(to: rhs.attributes)
}

Expand Down
30 changes: 7 additions & 23 deletions Source/UIContentSizeCategory+Scaling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,15 @@ import UIKit

internal extension UIContentSizeCategory {

var multiplier: CGFloat {
switch self {
case .accessibilityExtraExtraExtraLarge: return 23 / 16
case .accessibilityExtraExtraLarge: return 22 / 16
case .accessibilityExtraLarge: return 21 / 16
case .accessibilityLarge: return 20 / 16
case .accessibilityMedium: return 19 / 16
case .extraExtraExtraLarge: return 19 / 16
case .extraExtraLarge: return 18 / 16
case .extraLarge: return 17 / 16
case .large: return 1
case .medium: return 15 / 16
case .small: return 14 / 16
case .extraSmall: return 13 / 16
default: return 1
}
func scaledFontSize(forTextStyle style: UIFont.TextStyle) -> CGFloat {
let scaledFont = UIFont.preferredFont(forTextStyle: style,
compatibleWith: UITraitCollection(preferredContentSizeCategory: self))
return scaledFont.pointSize
}

func preferredContentSize(
_ base: CGFloat,
minSize: CGFloat = 0,
maxSize: CGFloat = CGFloat.greatestFiniteMagnitude
) -> CGFloat {
let result = base * multiplier
return min(max(result, minSize), maxSize)
func approximateScaleFactor(forTextStyle style: UIFont.TextStyle) -> CGFloat {
let defaultFontSize = UIContentSizeCategory.large.scaledFontSize(forTextStyle: style)
return scaledFontSize(forTextStyle: style) / defaultFontSize
}

}
4 changes: 0 additions & 4 deletions StyledTextKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
3D931C8520F412FC0090FFFB /* StyledTextRendererSnapshotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE0F20C339C800F283EA /* StyledTextRendererSnapshotTests.swift */; };
3D931C8620F412FF0090FFFB /* StyledTextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE0E20C339C800F283EA /* StyledTextTests.swift */; };
3D931C8720F413010090FFFB /* TextStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE1020C339C800F283EA /* TextStyleTests.swift */; };
3D931C8820F413040090FFFB /* UIContentSizeCategoryScalingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE0D20C339C800F283EA /* UIContentSizeCategoryScalingTests.swift */; };
3D931C8920F413060090FFFB /* UIFontUnionTraitsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEDFE20C339C800F283EA /* UIFontUnionTraitsTests.swift */; };
3D931C8A20F4130B0090FFFB /* LRUCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEDFD20C339C800F283EA /* LRUCacheTests.swift */; };
D9C656C0216B05DE00B02C6D /* UITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9C656BF216B05DE00B02C6D /* UITests.swift */; };
Expand Down Expand Up @@ -101,7 +100,6 @@
29BAEE0620C339C800F283EA /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
29BAEE0920C339C800F283EA /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
29BAEE0C20C339C800F283EA /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
29BAEE0D20C339C800F283EA /* UIContentSizeCategoryScalingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIContentSizeCategoryScalingTests.swift; sourceTree = "<group>"; };
29BAEE0E20C339C800F283EA /* StyledTextTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyledTextTests.swift; sourceTree = "<group>"; };
29BAEE0F20C339C800F283EA /* StyledTextRendererSnapshotTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyledTextRendererSnapshotTests.swift; sourceTree = "<group>"; };
29BAEE1020C339C800F283EA /* TextStyleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextStyleTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -225,7 +223,6 @@
29BAEE0F20C339C800F283EA /* StyledTextRendererSnapshotTests.swift */,
29BAEE0E20C339C800F283EA /* StyledTextTests.swift */,
29BAEE1020C339C800F283EA /* TextStyleTests.swift */,
29BAEE0D20C339C800F283EA /* UIContentSizeCategoryScalingTests.swift */,
29BAEDFE20C339C800F283EA /* UIFontUnionTraitsTests.swift */,
);
path = Tests;
Expand Down Expand Up @@ -575,7 +572,6 @@
3D931C8720F413010090FFFB /* TextStyleTests.swift in Sources */,
3D931C8920F413060090FFFB /* UIFontUnionTraitsTests.swift in Sources */,
3D931C8A20F4130B0090FFFB /* LRUCacheTests.swift in Sources */,
3D931C8820F413040090FFFB /* UIContentSizeCategoryScalingTests.swift in Sources */,
3D931C8520F412FC0090FFFB /* StyledTextRendererSnapshotTests.swift in Sources */,
3D931C8620F412FF0090FFFB /* StyledTextTests.swift in Sources */,
3D4040F520F40F2B00ED4B3D /* StyledTextBuilderTests.swift in Sources */,
Expand Down
34 changes: 0 additions & 34 deletions Tests/UIContentSizeCategoryScalingTests.swift

This file was deleted.