From a0ab806e9e07a5530602b60e523f9ae573cdd8f6 Mon Sep 17 00:00:00 2001 From: Lori M Olson Date: Thu, 28 Jan 2021 19:27:18 -0700 Subject: [PATCH 1/2] Fix template file --- ios/Fonts/Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/Fonts/Rakefile b/ios/Fonts/Rakefile index 9965176..09a0714 100644 --- a/ios/Fonts/Rakefile +++ b/ios/Fonts/Rakefile @@ -1,4 +1,5 @@ $:.unshift("/Library/RubyMotion/lib") +$:.unshift("~/.rubymotion/rubymotion-templates") require 'motion/project/template/ios' Motion::Project::App.setup do |app| From 1e32cc1b8918cb8b6afe30136ca0cec020d090ec Mon Sep 17 00:00:00 2001 From: Lori M Olson Date: Thu, 28 Jan 2021 19:30:08 -0700 Subject: [PATCH 2/2] Add dynamic_controller.rb, to illustrate the use of Dynamic Text with custom fonts in the app --- ios/Fonts/app/detail_controller.rb | 7 ++++ ios/Fonts/app/dynamic_controller.rb | 53 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 ios/Fonts/app/dynamic_controller.rb diff --git a/ios/Fonts/app/detail_controller.rb b/ios/Fonts/app/detail_controller.rb index 0e7d461..7acd9ef 100644 --- a/ios/Fonts/app/detail_controller.rb +++ b/ios/Fonts/app/detail_controller.rb @@ -12,6 +12,7 @@ def loadView def viewDidLoad navigationItem.title = "Sample Text" + navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemCompose, target: self, action: :display_font) end def viewWillAppear(animated) @@ -47,4 +48,10 @@ def sample_text After years of iOS work, RubyMotion seems like a thousand kittens playing the piano while sliding down a double-rainbow. EOS end + + def display_font + @dynamic_controller ||= DynamicController.alloc.init + @dynamic_controller.selected_font(@font_name) + self.navigationController.pushViewController(@dynamic_controller, animated:true) + end end diff --git a/ios/Fonts/app/dynamic_controller.rb b/ios/Fonts/app/dynamic_controller.rb new file mode 100644 index 0000000..d4540f7 --- /dev/null +++ b/ios/Fonts/app/dynamic_controller.rb @@ -0,0 +1,53 @@ + +class DynamicController < UIViewController + + TEXT_STYLES = [ + UIFontTextStyleBody, + UIFontTextStyleCallout, + UIFontTextStyleCaption1, + UIFontTextStyleCaption2, + UIFontTextStyleFootnote, + UIFontTextStyleHeadline, + UIFontTextStyleSubheadline, + UIFontTextStyleLargeTitle, + UIFontTextStyleTitle1, + UIFontTextStyleTitle2, + UIFontTextStyleTitle3, + ] + + def loadView + self.view = UIScrollView.alloc.initWithFrame(UIScreen.mainScreen.applicationFrame) + view.backgroundColor = UIColor.whiteColor + @labels = TEXT_STYLES.map do |style| + label = UILabel.alloc.init + view.addSubview(label) + {:label => label, :style => style, size: UIFont.preferredFontForTextStyle(style).pointSize} + end + end + + def viewDidLoad + navigationItem.title = "Dynamic Text - #{@font_name}" + end + + def viewWillAppear(_) + + top = 100 + @labels.each do |label_style| + metrics = UIFontMetrics.metricsForTextStyle(label_style[:style]) + label_style[:label].font = metrics.scaledFontForFont(UIFont.fontWithName(@font_name, size: label_style[:size])) + label_style[:label].text = label_style[:style] + label_style[:label].adjustsFontForContentSizeCategory = true + + label_style[:label].frame = [ + [10, top], [ + view.frame.size.width - 20, label_style[:label].font.pointSize + 5] + ] + top += label_style[:label].font.pointSize + 20 + end + end + + def selected_font(font_name) + @font_name = font_name + end + +end