[TextFields] Typography Themer implementation.
* TextFields Typography Themer added.
Tests and example included.
* Removing extra checks, comment the reason behind checking pointsize
* fixing typo.
MODIFY
MaterialComponents.podspec
MaterialComponents.podspec
@@ -681,6 +681,14 @@ end
extension.dependency "MaterialComponents/TextFields"
extension.dependency "MaterialComponents/Themes"
end
+ component.subspec "TypographyThemer" do |extension|
+ extension.ios.deployment_target = '8.0'
+ extension.public_header_files = "components/TextFields/src/#{extension.base_name}/*.h"
+ extension.source_files = "components/TextFields/src/#{extension.base_name}/*.{h,m}"
+
+ extension.dependency "MaterialComponents/TextFields"
+ extension.dependency "MaterialComponents/schemes/Typography"
+ end
end

MODIFY
components/TextFields/BUILD
components/TextFields/BUILD
@@ -64,6 +64,21 @@ mdc_objc_library(
visibility = ["//visibility:public"],
)
+mdc_objc_library(
+ name = "TypographyThemer",
+ srcs = native.glob(["src/TypographyThemer/*.m"]),
+ hdrs = native.glob(["src/TypographyThemer/*.h"]),
+ includes = ["src/TypographyThemer"],
+ sdk_frameworks = [
+ "UIKit",
+ ],
+ deps = [
+ ":TextFields",
+ "//components/schemes/Typography",
+ ],
+ visibility = ["//visibility:public"],
+)
+
mdc_objc_library(
name = "unit_test_sources",
testonly = 1,
@@ -77,6 +92,7 @@ mdc_objc_library(
":TextFields",
":ColorThemer",
":FontThemer",
+ ":TypographyThemer",
],
visibility = ["//visibility:private"],
)

MODIFY
components/TextFields/examples/TextFieldFilledExample.swift
components/TextFields/examples/TextFieldFilledExample.swift
@@ -282,7 +282,10 @@ final class TextFieldFilledSwiftExample: UIViewController {
func style(textInputController : MDCTextInputController) {
let colorScheme = MDCSemanticColorScheme()
MDCTextFieldColorThemer.applySemanticColorScheme(colorScheme, to:textInputController)
+ let typographyScheme = MDCTypographyScheme()
+ MDCTextFieldTypographyThemer.applyTypographyScheme(typographyScheme, to: textInputController)
}
+
func addGestureRecognizer() {
let tapRecognizer = UITapGestureRecognizer(target: self,
action: #selector(tapDidTouch(sender: )))

ADD
components/TextFields/src/TypographyThemer/MDCTextFieldTypographyThemer.h
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+#import "MaterialTextFields.h"
+#import "MaterialTypographyScheme.h"
+
+
+/**
+ Used to apply a typography scheme to theme an MDCTextField/MDCTextInputController.
+ */
+@interface MDCTextFieldTypographyThemer : NSObject
+
+/**
+ Applies a typography scheme to theme an MDCTextInputController instance.
+
+ @param typographyScheme The typography scheme that applies to a MDCTextInputController.
+ @param textInputController A MDCTextInputController instance that typography scheme will be applied
+ to.
+ */
++ (void)applyTypographyScheme:(nonnull id<MDCTypographyScheming>)typographyScheme
+ toTextInputController:(nonnull id<MDCTextInputController>)textInputController;
+
+/**
+ Applies a typography scheme to theme an specific class type responding to MDCTextInputController
+ protocol. Will not apply to existing instances.
+
+ @param typographyScheme The typography scheme that applies to a MDCTextInputController.
+ @param textInputControllerClass A MDCTextInputController class that typography scheme will be
+ applied to.
+ */
++ (void)applyTypographyScheme:(nonnull id<MDCTypographyScheming>)typographyScheme
+ toAllTextInputControllersOfClass:(nonnull Class<MDCTextInputController>)textInputControllerClass
+ NS_SWIFT_NAME(apply(_:toAllControllersOfClass:));
+
+/**
+ Applies a typography scheme to a MDCTextField instance.
+
+ @param typographyScheme The typography scheme that applies to an MDCTextField.
+ @param textInput An MDCTextInput instance that typography scheme will be applied to.
+ */
++ (void)applyTypographyScheme:(nonnull id<MDCTypographyScheming>)typographyScheme
+ toTextInput:(nonnull id<MDCTextInput>)textInput;
+
+@end

ADD
components/TextFields/src/TypographyThemer/MDCTextFieldTypographyThemer.m
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+#import "MDCTextFieldTypographyThemer.h"
+
+@implementation MDCTextFieldTypographyThemer
+
++ (void)applyTypographyScheme:(id<MDCTypographyScheming>)typographyScheme
+ toTextInputController:(id<MDCTextInputController>)textInputController {
+ textInputController.inlinePlaceholderFont = typographyScheme.subtitle1;
+ textInputController.leadingUnderlineLabelFont = typographyScheme.caption;
+ textInputController.trailingUnderlineLabelFont = typographyScheme.caption;
+ if ([textInputController
+ conformsToProtocol:@protocol(MDCTextInputControllerFloatingPlaceholder)]) {
+ id<MDCTextInputControllerFloatingPlaceholder> textInputControllerFloatingPlaceholder =
+ (id<MDCTextInputControllerFloatingPlaceholder>)textInputController;
+
+ // if caption.pointSize <= 0 there is no meaningful ratio so we fallback to default.
+ if (typographyScheme.caption.pointSize <= 0) {
+ textInputControllerFloatingPlaceholder.floatingPlaceholderScale = nil;
+ } else {
+ double ratio = typographyScheme.caption.pointSize/typographyScheme.subtitle1.pointSize;
+ textInputControllerFloatingPlaceholder.floatingPlaceholderScale =
+ [NSNumber numberWithDouble:ratio];
+ }
+ }
+}
+
++ (void)applyTypographyScheme:(id<MDCTypographyScheming>)typographyScheme
+ toTextInput:(id<MDCTextInput>)textInput {
+ textInput.font = typographyScheme.subtitle1;
+ textInput.placeholderLabel.font = typographyScheme.subtitle1;
+ textInput.leadingUnderlineLabel.font = typographyScheme.caption;
+ textInput.trailingUnderlineLabel.font = typographyScheme.caption;
+}
+
+// TODO: (larche) Drop this "#if !defined..." and the pragmas when we drop Xcode 8 support.
+// This is to silence a warning that doesn't appear in Xcode 9 when you use Class as an object.
+#if !defined(__IPHONE_11_0)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wobjc-method-access"
+#endif
++ (void)applyTypographyScheme:(id<MDCTypographyScheming>)typographyScheme
+ toAllTextInputControllersOfClass:(Class<MDCTextInputController>)textInputControllerClass {
+ [textInputControllerClass setInlinePlaceholderFontDefault:typographyScheme.subtitle1];
+ [textInputControllerClass setTrailingUnderlineLabelFontDefault:typographyScheme.caption];
+ [textInputControllerClass setLeadingUnderlineLabelFontDefault:typographyScheme.caption];
+ if ([textInputControllerClass
+ conformsToProtocol:@protocol(MDCTextInputControllerFloatingPlaceholder)]) {
+ Class<MDCTextInputControllerFloatingPlaceholder> textInputControllerFloatingPlaceholderClass =
+ (Class<MDCTextInputControllerFloatingPlaceholder>)textInputControllerClass;
+ // if caption.pointSize <= 0 there is no meaningful ratio so we fallback to default.
+ if (typographyScheme.caption.pointSize <= 0) {
+ [textInputControllerFloatingPlaceholderClass setFloatingPlaceholderScaleDefault:0];
+ } else {
+ CGFloat scale = typographyScheme.caption.pointSize/typographyScheme.subtitle1.pointSize;
+ [textInputControllerFloatingPlaceholderClass setFloatingPlaceholderScaleDefault:scale];
+ }
+ }
+}
+#if !defined(__IPHONE_11_0)
+#pragma clang diagnostic pop
+#endif
+
+
+@end

ADD
components/TextFields/tests/unit/TextFieldTypographyThemer.m
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+#import <XCTest/XCTest.h>
+
+#import "MaterialTextFields.h"
+#import "MaterialThemes.h"
+#import "MDCTextFieldTypographyThemer.h"
+
+@interface MDCTextFieldTypographyThemer (ResetDefaults)
+
++ (void)resetDefaultsForClass:(Class<MDCTextInputController>)class;
+
+@end
+
+@implementation MDCTextFieldTypographyThemer (ResetDefaults)
+
+// TODO: (larche) Drop this if defined and the pragmas when we drop Xcode 8 support.
+// This is to silence a warning that doesn't appear in Xcode 9 when you use Class as an object.
+#if !defined(__IPHONE_11_0)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wobjc-method-access"
+#endif
++ (void)resetDefaultsForClass:(Class<MDCTextInputController>)textInputControllerClass {
+ [textInputControllerClass setInlinePlaceholderFontDefault:nil];
+ [textInputControllerClass setTrailingUnderlineLabelFontDefault:nil];
+ [textInputControllerClass setLeadingUnderlineLabelFontDefault:nil];
+
+ if ([textInputControllerClass
+ conformsToProtocol:@protocol(MDCTextInputControllerFloatingPlaceholder)]) {
+ Class<MDCTextInputControllerFloatingPlaceholder> textInputControllerFloatingPlaceholderClass =
+ (Class<MDCTextInputControllerFloatingPlaceholder>)textInputControllerClass;
+ [textInputControllerFloatingPlaceholderClass setFloatingPlaceholderScaleDefault:0];
+ }
+}
+#if !defined(__IPHONE_11_0)
+#pragma clang diagnostic pop
+#endif
+
+@end
+
+@interface TextFieldTypographyThemer : XCTestCase
+
+@end
+
+@implementation TextFieldTypographyThemer
+
+- (void)tearDown {
+ [super tearDown];
+ [MDCTextFieldTypographyThemer resetDefaultsForClass:[MDCTextInputControllerFullWidth class]];
+ [MDCTextFieldTypographyThemer resetDefaultsForClass:[MDCTextInputControllerBase class]];
+}
+
+- (void)testTypographyThemerSetsTheFontsForTextFieldProperly {
+ MDCTextField *textField = [[MDCTextField alloc] initWithFrame:CGRectZero];
+ textField.font = [UIFont systemFontOfSize:28];
+ textField.leadingUnderlineLabel.font = [UIFont systemFontOfSize:28];
+ textField.trailingUnderlineLabel.font = [UIFont systemFontOfSize:28];
+ textField.placeholderLabel.font = [UIFont systemFontOfSize:28];
+ MDCTypographyScheme *typographyScheme = [[MDCTypographyScheme alloc] init];
+ [MDCTextFieldTypographyThemer applyTypographyScheme:typographyScheme toTextInput:textField];
+ XCTAssertEqualObjects(textField.leadingUnderlineLabel.font, typographyScheme.caption);
+ XCTAssertEqualObjects(textField.trailingUnderlineLabel.font, typographyScheme.caption);
+ XCTAssertEqualObjects(textField.placeholderLabel.font, typographyScheme.subtitle1);
+ XCTAssertEqualObjects(textField.font, typographyScheme.subtitle1);
+}
+
+- (void)testTypographyThemerSetsTheFontsForTextInputControllerClass {
+ MDCTypographyScheme *typographyScheme = [[MDCTypographyScheme alloc] init];
+ [MDCTextFieldTypographyThemer applyTypographyScheme:typographyScheme
+ toAllTextInputControllersOfClass:[MDCTextInputControllerFullWidth class]];
+ XCTAssertEqualObjects([MDCTextInputControllerFullWidth trailingUnderlineLabelFontDefault],
+ typographyScheme.caption);
+ XCTAssertEqualObjects([MDCTextInputControllerFullWidth inlinePlaceholderFontDefault],
+ typographyScheme.subtitle1);
+}
+
+- (void)testTypographyThemerSetsTheFontsForTextInputControllerFloatingPlaceHolderClass {
+ MDCTypographyScheme *typographyScheme = [[MDCTypographyScheme alloc] init];
+ [MDCTextFieldTypographyThemer applyTypographyScheme:typographyScheme
+ toAllTextInputControllersOfClass:[MDCTextInputControllerBase class]];
+ XCTAssertEqualObjects([MDCTextInputControllerBase trailingUnderlineLabelFontDefault],
+ typographyScheme.caption);
+ XCTAssertEqualObjects([MDCTextInputControllerBase leadingUnderlineLabelFontDefault],
+ typographyScheme.caption);
+ XCTAssertEqualObjects([MDCTextInputControllerBase inlinePlaceholderFontDefault],
+ typographyScheme.subtitle1);
+ XCTAssertEqual([MDCTextInputControllerBase floatingPlaceholderScaleDefault],
+ typographyScheme.caption.pointSize/typographyScheme.subtitle1.pointSize);
+}
+
+- (void)testTypographyThemerSetsTheFontsForTextInputControllerInstance {
+ MDCTextInputControllerFullWidth *inputController = [[MDCTextInputControllerFullWidth alloc] init];
+ inputController.trailingUnderlineLabelFont = [UIFont systemFontOfSize:80];
+ inputController.inlinePlaceholderFont = [UIFont systemFontOfSize:80];
+ MDCTypographyScheme *typographyScheme = [[MDCTypographyScheme alloc] init];
+ [MDCTextFieldTypographyThemer applyTypographyScheme:typographyScheme
+ toTextInputController:inputController];
+ XCTAssertEqualObjects(inputController.trailingUnderlineLabelFont, typographyScheme.caption);
+ XCTAssertEqualObjects(inputController.inlinePlaceholderFont, typographyScheme.subtitle1);
+}
+
+- (void)testTypographyThemerSetsTheFontsForTextInputControllerFloatingPlaceHolderInstance {
+ MDCTextInputControllerBase *floatingInputController = [[MDCTextInputControllerBase alloc] init];
+ floatingInputController.trailingUnderlineLabelFont = [UIFont systemFontOfSize:80];
+ floatingInputController.leadingUnderlineLabelFont = [UIFont systemFontOfSize:80];
+ floatingInputController.inlinePlaceholderFont = [UIFont systemFontOfSize:80];
+ MDCTypographyScheme *typographyScheme = [[MDCTypographyScheme alloc] init];
+ [MDCTextFieldTypographyThemer applyTypographyScheme:typographyScheme
+ toTextInputController:floatingInputController];
+ XCTAssertEqualObjects(floatingInputController.trailingUnderlineLabelFont,
+ typographyScheme.caption);
+ XCTAssertEqualObjects(floatingInputController.leadingUnderlineLabelFont,
+ typographyScheme.caption);
+ XCTAssertEqualObjects(floatingInputController.inlinePlaceholderFont, typographyScheme.subtitle1);
+ XCTAssertEqual([floatingInputController.floatingPlaceholderScale doubleValue],
+ typographyScheme.caption.pointSize/typographyScheme.subtitle1.pointSize);
+}
+
+@end

[TextFields] Typography Themer implementation. (#3303)

* TextFields Typography Themer added.
Tests and example included.

* Removing extra checks, comment the reason behind checking pointsize

* fixing typo.