Skip to content

Adds an example controller/view using dynamic text styles #70

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions ios/Fonts/Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$:.unshift("/Library/RubyMotion/lib")
$:.unshift("~/.rubymotion/rubymotion-templates")
require 'motion/project/template/ios'

Motion::Project::App.setup do |app|
7 changes: 7 additions & 0 deletions ios/Fonts/app/detail_controller.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions ios/Fonts/app/dynamic_controller.rb
Original file line number Diff line number Diff line change
@@ -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