diff --git a/WordPressShared.podspec b/WordPressShared.podspec index bf46f8f..2898202 100644 --- a/WordPressShared.podspec +++ b/WordPressShared.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "WordPressShared" - s.version = "1.10.0-beta.2" + s.version = "1.12.0" s.summary = "Shared components used in building the WordPress iOS apps and other library components." s.description = <<-DESC diff --git a/WordPressShared.xcodeproj/project.pbxproj b/WordPressShared.xcodeproj/project.pbxproj index a4398e7..c79adf5 100644 --- a/WordPressShared.xcodeproj/project.pbxproj +++ b/WordPressShared.xcodeproj/project.pbxproj @@ -82,6 +82,7 @@ B5A7881F202B3A92007874FB /* WPTextFieldTableViewCell.h in Headers */ = {isa = PBXBuildFile; fileRef = B5A7881B202B3A92007874FB /* WPTextFieldTableViewCell.h */; settings = {ATTRIBUTES = (Public, ); }; }; B5A78820202B3A92007874FB /* WPTextFieldTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = B5A7881C202B3A92007874FB /* WPTextFieldTableViewCell.m */; }; B5A78821202B3A92007874FB /* WPTableViewCell.h in Headers */ = {isa = PBXBuildFile; fileRef = B5A7881D202B3A92007874FB /* WPTableViewCell.h */; settings = {ATTRIBUTES = (Public, ); }; }; + CE1CB30424DCAFCB00DBAD9C /* CollectionType+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE1CB30324DCAFCB00DBAD9C /* CollectionType+Helpers.swift */; }; E157E126239527700051AE41 /* Secret.swift in Sources */ = {isa = PBXBuildFile; fileRef = E157E125239527700051AE41 /* Secret.swift */; }; E157E128239527AD0051AE41 /* SecretTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E157E127239527AD0051AE41 /* SecretTests.swift */; }; E18EABEA1F0E2C6800BFCB0B /* TestAnalyticsTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = E18EABE81F0E2C6800BFCB0B /* TestAnalyticsTracker.m */; }; @@ -202,6 +203,7 @@ B5A7881D202B3A92007874FB /* WPTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPTableViewCell.h; sourceTree = ""; }; B5A78823202B3B3C007874FB /* WordPressUIKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = WordPressUIKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; BAD47F9FCABF8C12D6352A20 /* Pods-WordPressSharedTests.release-internal.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressSharedTests.release-internal.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressSharedTests/Pods-WordPressSharedTests.release-internal.xcconfig"; sourceTree = ""; }; + CE1CB30324DCAFCB00DBAD9C /* CollectionType+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CollectionType+Helpers.swift"; sourceTree = ""; }; D62D565031D4683172615BAC /* Pods-WordPressSharedTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressSharedTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressSharedTests/Pods-WordPressSharedTests.release.xcconfig"; sourceTree = ""; }; E157E125239527700051AE41 /* Secret.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secret.swift; sourceTree = ""; }; E157E127239527AD0051AE41 /* SecretTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretTests.swift; sourceTree = ""; }; @@ -297,6 +299,7 @@ 82706FE01ECA32D400155CBF /* Utility */ = { isa = PBXGroup; children = ( + CE1CB30324DCAFCB00DBAD9C /* CollectionType+Helpers.swift */, 74FA25F21F1FD9640044BC54 /* DateUtils.h */, 74FA25F31F1FD9640044BC54 /* DateUtils.m */, F1134A262270C15E00B8F75F /* Debouncer.swift */, @@ -571,11 +574,11 @@ }; buildConfigurationList = 829DD1381EC9EED200AB8C12 /* Build configuration list for PBXProject "WordPressShared" */; compatibilityVersion = "Xcode 3.2"; - developmentRegion = English; + developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( - English, en, + Base, ); mainGroup = 829DD1341EC9EED200AB8C12; productRefGroup = 829DD13F1EC9EED200AB8C12 /* Products */; @@ -712,6 +715,7 @@ F193585624CA3F4E00942507 /* AnalyticsEvent.swift in Sources */, F1134A272270C15E00B8F75F /* Debouncer.swift in Sources */, B5393FD8206D608F007BF9D4 /* EmailFormatValidator.swift in Sources */, + CE1CB30424DCAFCB00DBAD9C /* CollectionType+Helpers.swift in Sources */, F106FA62226FA82E00706DE4 /* String+URLValidation.swift in Sources */, F10A569023E1FC1300B184F4 /* String+StripShortcodes.swift in Sources */, 32E1BFD524A63DE6007A08F0 /* WPStyleGuide+SerifFonts.swift in Sources */, diff --git a/WordPressShared/Core/Utility/CollectionType+Helpers.swift b/WordPressShared/Core/Utility/CollectionType+Helpers.swift new file mode 100644 index 0000000..c0d76cc --- /dev/null +++ b/WordPressShared/Core/Utility/CollectionType+Helpers.swift @@ -0,0 +1,21 @@ +import Foundation + + +// MARK: - Collection Type Helpers +// +extension BidirectionalCollection { + public func lastIndex(where predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.Index? { + if let idx = try reversed().firstIndex(where: predicate) { + return self.index(before: idx.base) + } + return nil + } +} + +extension Collection { + /// Returns the element at the specified index if it is within bounds, otherwise nil. + /// + public subscript (safe index: Index) -> Element? { + return indices.contains(index) ? self[index] : nil + } +} diff --git a/WordPressShared/Core/Views/WPStyleGuide+DynamicType.swift b/WordPressShared/Core/Views/WPStyleGuide+DynamicType.swift index 804a71d..64ef5bf 100644 --- a/WordPressShared/Core/Views/WPStyleGuide+DynamicType.swift +++ b/WordPressShared/Core/Views/WPStyleGuide+DynamicType.swift @@ -189,6 +189,28 @@ extension WPStyleGuide { return UIFont(descriptor: descriptorWithTraits, size: size) } + /// Creates a NotoSerif UIFont at the specified size. + /// + /// - Parameters: + /// - size: The desired size. + /// + /// - Returns: The created font. + /// + @objc public class func fixedNotoFontWithSize(_ size: CGFloat) -> UIFont { + return fixedCustomNotoFontNamed("NotoSerif", withSize: size) + } + + /// Creates a bold NotoSerif UIFont at the specified size. + /// + /// - Parameters: + /// - size: The desired size. + /// + /// - Returns: The created font. + /// + @objc public class func fixedBoldNotoFontWithSize(_ size: CGFloat) -> UIFont { + return fixedCustomNotoFontNamed("NotoSerif-Bold", withSize: size) + } + /// Creates a NotoSerif UIFont for the user current text size settings. /// /// - Parameters: @@ -239,7 +261,7 @@ extension WPStyleGuide { /// - fontName: the Noto font name (NotoSerif, NotoSerif-Bold, NotoSerif-Italic, NotoSerif-BoldItalic) /// - style: The desired UIFontTextStyle. /// - /// - Returns: The created font point size. + /// - Returns: The created font. /// private class func customNotoFontNamed(_ fontName: String, forTextStyle style: UIFont.TextStyle, maximumPointSize: CGFloat = maxFontSize) -> UIFont { WPFontManager.loadNotoFontFamily() @@ -251,4 +273,25 @@ extension WPStyleGuide { } return font } + + /// Creates a Noto UIFont at the specified size. + /// + /// - Parameters: + /// - fontName: the Noto font name (NotoSerif, NotoSerif-Bold, NotoSerif-Italic, NotoSerif-BoldItalic) + /// - size: The desired point size. + /// + /// - Returns: The created font. + /// + private class func fixedCustomNotoFontNamed(_ fontName: String, withSize size: CGFloat) -> UIFont { + WPFontManager.loadNotoFontFamily() + guard let font = UIFont(name: fontName, size: size) else { + // If we can't get the Noto font for some reason we will default to the system font + if fontName.contains("Bold") { + return UIFont.systemFont(ofSize: size, weight: .bold) + } else { + return UIFont.systemFont(ofSize: size) + } + } + return font + } } diff --git a/WordPressShared/Core/Views/WPStyleGuide+SerifFonts.swift b/WordPressShared/Core/Views/WPStyleGuide+SerifFonts.swift index 5d35647..fdda3b3 100644 --- a/WordPressShared/Core/Views/WPStyleGuide+SerifFonts.swift +++ b/WordPressShared/Core/Views/WPStyleGuide+SerifFonts.swift @@ -15,7 +15,27 @@ extension WPStyleGuide { return UIFontMetrics.default.scaledFont(for: UIFont(descriptor: fontDescriptor, size: 0.0)) } - + // Returns the system serif font (New York) for iOS 13+ but defaults to noto for older os's, at the default size for the specified style + @objc public class func fixedSerifFontForTextStyle(_ style: UIFont.TextStyle, + fontWeight weight: UIFont.Weight = .regular) -> UIFont { + + let defaultContentSizeCategory = UITraitCollection(preferredContentSizeCategory: .large) // .large is the default + let fontSize = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style, compatibleWith: defaultContentSizeCategory).pointSize + + guard #available(iOS 13, *), + let fontDescriptor = UIFont.systemFont(ofSize: fontSize, weight: weight).fontDescriptor.withDesign(.serif) else { + switch weight { + case .bold, .semibold, .heavy, .black: + return WPStyleGuide.fixedBoldNotoFontWithSize(fontSize) + default: + return WPStyleGuide.fixedNotoFontWithSize(fontSize) + } + } + + // Uses size from original font, so we don't want to override it here. + return UIFont(descriptor: fontDescriptor, size: 0.0) + } + private class func notoFontForTextStyle(_ style: UIFont.TextStyle, fontWeight weight: UIFont.Weight = .regular) -> UIFont { var font: UIFont