Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit d06ae77

Browse files
committed
Merged PR 414972: Remove FontDescriptor extension
Move the function to change the weight of the preferred text style font to MSFonts. Make sure demo labels are showing all of its description text based on current device content size category.
1 parent 316f1ee commit d06ae77

File tree

5 files changed

+38
-41
lines changed

5 files changed

+38
-41
lines changed

OfficeUIFabric.Demo/OfficeUIFabric.Demo/Demos/MSLabelDemoController.swift

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import OfficeUIFabric
77
import UIKit
88

99
class MSLabelDemoController: DemoController {
10+
private var dynamicLabels = [MSLabel]()
11+
1012
override func viewDidLoad() {
1113
super.viewDidLoad()
1214

1315
addLabel(text: "Text Styles", style: .headline, colorStyle: .regular).textAlignment = .center
1416
for style in MSTextStyle.allCases {
15-
addLabel(text: style.detailedDescription, style: style, colorStyle: .regular)
17+
dynamicLabels.append(addLabel(text: style.detailedDescription, style: style, colorStyle: .regular))
1618
}
1719

1820
container.addArrangedSubview(UIView()) // spacer
@@ -23,18 +25,27 @@ class MSLabelDemoController: DemoController {
2325
}
2426

2527
container.addArrangedSubview(UIView()) // spacer
28+
29+
NotificationCenter.default.addObserver(self, selector: #selector(handleContentSizeCategoryDidChange), name: UIContentSizeCategory.didChangeNotification, object: nil)
2630
}
2731

2832
@discardableResult
2933
func addLabel(text: String, style: MSTextStyle, colorStyle: MSTextColorStyle) -> MSLabel {
3034
let label = MSLabel(style: style, colorStyle: colorStyle)
3135
label.text = text
36+
label.numberOfLines = 0
3237
if colorStyle == .white {
3338
label.backgroundColor = .black
3439
}
3540
container.addArrangedSubview(label)
3641
return label
3742
}
43+
44+
@objc private func handleContentSizeCategoryDidChange() {
45+
for label in dynamicLabels {
46+
label.text = label.style.detailedDescription
47+
}
48+
}
3849
}
3950

4051
extension MSTextColorStyle {
@@ -108,3 +119,13 @@ extension MSTextStyle {
108119
return "\(description) is \(weight) \(Int(font.pointSize))pt"
109120
}
110121
}
122+
123+
extension UIFontDescriptor {
124+
var weight: UIFont.Weight {
125+
let traits = object(forKey: .traits) as? [UIFontDescriptor.TraitKey: Any]
126+
if let weight = traits?[.weight] as? NSNumber {
127+
return UIFont.Weight(CGFloat(weight.floatValue))
128+
}
129+
return .regular
130+
}
131+
}

OfficeUIFabric.xcodeproj/project.pbxproj

-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
8FD011A3228A82A600D25925 /* DateComponents+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD9A5C862179464F00D224D9 /* DateComponents+Extensions.swift */; };
7272
8FD011A6228A82A600D25925 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A559BB7D212B6D100055E107 /* String+Extension.swift */; };
7373
8FD011A8228A82A600D25925 /* UIFont+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B87AF0211BD4380038C37C /* UIFont+Extension.swift */; };
74-
8FD011A9228A82A600D25925 /* UIFontDescriptor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B87AEE211BCA710038C37C /* UIFontDescriptor+Extension.swift */; };
7574
8FD011AA228A82A600D25925 /* UIImage+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5961FA6218A2E4500E2A506 /* UIImage+Extensions.swift */; };
7675
8FD011AC228A82A600D25925 /* UIScreen+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B87B01211E20B50038C37C /* UIScreen+Extension.swift */; };
7776
8FD011AD228A82A600D25925 /* UITableViewCell+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = B444D6B02181403C0002B4D4 /* UITableViewCell+Extension.swift */; };
@@ -121,7 +120,6 @@
121120
A5961FA7218A2E4500E2A506 /* UIImage+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5961FA6218A2E4500E2A506 /* UIImage+Extensions.swift */; };
122121
A5B6617323A41E2900E801DD /* MSNotificationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B6617223A41E2900E801DD /* MSNotificationView.swift */; };
123122
A5B6617423A41E2900E801DD /* MSNotificationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B6617223A41E2900E801DD /* MSNotificationView.swift */; };
124-
A5B87AEF211BCA710038C37C /* UIFontDescriptor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B87AEE211BCA710038C37C /* UIFontDescriptor+Extension.swift */; };
125123
A5B87AF1211BD4380038C37C /* UIFont+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B87AF0211BD4380038C37C /* UIFont+Extension.swift */; };
126124
A5B87AF6211E16370038C37C /* MSDrawerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B87AF3211E16360038C37C /* MSDrawerController.swift */; };
127125
A5B87AF7211E16370038C37C /* MSDrawerTransitionAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5B87AF4211E16360038C37C /* MSDrawerTransitionAnimator.swift */; };
@@ -292,7 +290,6 @@
292290
A5961FA4218A260500E2A506 /* MSPopupMenuSectionHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSPopupMenuSectionHeaderView.swift; sourceTree = "<group>"; };
293291
A5961FA6218A2E4500E2A506 /* UIImage+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Extensions.swift"; sourceTree = "<group>"; };
294292
A5B6617223A41E2900E801DD /* MSNotificationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSNotificationView.swift; sourceTree = "<group>"; };
295-
A5B87AEE211BCA710038C37C /* UIFontDescriptor+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFontDescriptor+Extension.swift"; sourceTree = "<group>"; };
296293
A5B87AF0211BD4380038C37C /* UIFont+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+Extension.swift"; sourceTree = "<group>"; };
297294
A5B87AF3211E16360038C37C /* MSDrawerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MSDrawerController.swift; sourceTree = "<group>"; };
298295
A5B87AF4211E16360038C37C /* MSDrawerTransitionAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MSDrawerTransitionAnimator.swift; sourceTree = "<group>"; };
@@ -540,7 +537,6 @@
540537
A559BB7D212B6D100055E107 /* String+Extension.swift */,
541538
A56CE7B522E68A7800AA77EE /* UIColor+Extensions.swift */,
542539
A5B87AF0211BD4380038C37C /* UIFont+Extension.swift */,
543-
A5B87AEE211BCA710038C37C /* UIFontDescriptor+Extension.swift */,
544540
A5961FA6218A2E4500E2A506 /* UIImage+Extensions.swift */,
545541
A5F3B148232B1F9700007A4F /* UIResponder+Extensions.swift */,
546542
A5B87B01211E20B50038C37C /* UIScreen+Extension.swift */,
@@ -1180,7 +1176,6 @@
11801176
FD9DA7B6232C33A80013E41B /* UIViewController+Navigation.swift in Sources */,
11811177
8FD011A6228A82A600D25925 /* String+Extension.swift in Sources */,
11821178
8FD011A8228A82A600D25925 /* UIFont+Extension.swift in Sources */,
1183-
8FD011A9228A82A600D25925 /* UIFontDescriptor+Extension.swift in Sources */,
11841179
8FD011AA228A82A600D25925 /* UIImage+Extensions.swift in Sources */,
11851180
497DC2DC24185885008D86F8 /* MSPillButton.swift in Sources */,
11861181
8FD011AC228A82A600D25925 /* UIScreen+Extension.swift in Sources */,
@@ -1246,7 +1241,6 @@
12461241
FD56FD962192754B0023C7EA /* MSDateTimePickerViewComponent.swift in Sources */,
12471242
FDFB8AF021361C9D0046850A /* MSCalendarViewDayTodayCell.swift in Sources */,
12481243
FD41C88422DD13230086F899 /* MSContentScrollViewTraits.swift in Sources */,
1249-
A5B87AEF211BCA710038C37C /* UIFontDescriptor+Extension.swift in Sources */,
12501244
FD97580F2191118E00B67319 /* MSDateTimePickerViewComponentCell.swift in Sources */,
12511245
A5B6617323A41E2900E801DD /* MSNotificationView.swift in Sources */,
12521246
FD41C88622DD13230086F899 /* MSShyHeaderController.swift in Sources */,

OfficeUIFabric/Core/Fonts.swift

+15-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public final class MSFonts: NSObject {
1111
/// Bold 26pt - Does not scale automatically with Dynamic Type
1212
@objc public static let title1 = UIFont.systemFont(ofSize: 26, weight: .bold)
1313
/// Semibold 22pt
14-
@objc public static var title2: UIFont { return UIFont.preferredFont(forTextStyle: .title2).withWeight(.semibold) }
14+
@objc public static var title2: UIFont { return preferredFont(forTextStyle: .title2, weight: .semibold) }
1515
/// Semibold 17pt
1616
@objc public static var headline: UIFont { return .preferredFont(forTextStyle: .headline) }
1717
@objc public static var headlineUnscaled: UIFont { return .preferredFont(forTextStyle: .headline, compatibleWith: UITraitCollection(preferredContentSizeCategory: .large)) }
@@ -24,9 +24,9 @@ public final class MSFonts: NSObject {
2424
@objc public static var footnote: UIFont { return .preferredFont(forTextStyle: .footnote) }
2525
@objc public static var footnoteUnscaled: UIFont { return .preferredFont(forTextStyle: .footnote, compatibleWith: UITraitCollection(preferredContentSizeCategory: .large)) }
2626
/// Medium 15pt
27-
@objc public static var button1: UIFont { return UIFont.preferredFont(forTextStyle: .subheadline).withWeight(.medium) }
27+
@objc public static var button1: UIFont { return preferredFont(forTextStyle: .subheadline, weight: .medium) }
2828
/// Medium 13pt
29-
@objc public static var button2: UIFont { return UIFont.preferredFont(forTextStyle: .footnote).withWeight(.medium) }
29+
@objc public static var button2: UIFont { return preferredFont(forTextStyle: .footnote, weight: .medium) }
3030
/// Medium 10pt - Does not scale automatically with Dynamic Type
3131
@objc public static let button3 = UIFont.systemFont(ofSize: 10, weight: .medium)
3232
/// Medium 15pt - Does not scale automatically with Dynamic Type
@@ -36,6 +36,18 @@ public final class MSFonts: NSObject {
3636
/// Regular 11pt
3737
@objc public static var caption2: UIFont { return .preferredFont(forTextStyle: .caption2) }
3838

39+
static func preferredFont(forTextStyle style: UIFont.TextStyle, weight: UIFont.Weight, size: CGFloat? = 0) -> UIFont {
40+
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style)
41+
let newDescriptor = descriptor.addingAttributes([.traits: [UIFontDescriptor.TraitKey.weight: weight]])
42+
let pointSize: CGFloat
43+
if let size = size, size > 0 {
44+
pointSize = size
45+
} else {
46+
pointSize = descriptor.pointSize
47+
}
48+
return UIFont(descriptor: newDescriptor, size: pointSize)
49+
}
50+
3951
private override init() {
4052
super.init()
4153
}

OfficeUIFabric/Extensions/UIFont+Extension.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
import UIKit
77

8-
public extension UIFont {
8+
extension UIFont {
99
var deviceLineHeight: CGFloat { return UIScreen.main.roundToDevicePixels(lineHeight) }
1010
var deviceLineHeightWithLeading: CGFloat { return UIScreen.main.roundToDevicePixels(lineHeight + max(0, leading)) }
11-
12-
func withWeight(_ weight: UIFont.Weight) -> UIFont {
13-
return UIFont(descriptor: fontDescriptor.withWeight(weight), size: pointSize)
14-
}
1511
}

OfficeUIFabric/Extensions/UIFontDescriptor+Extension.swift

-26
This file was deleted.

0 commit comments

Comments
 (0)