Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Fonts sample (contributed by Samuel Chandra)

  • Loading branch information...
commit 4465adbfca51e6c7066e248bc349b15b8b866079 1 parent 7c799cd
Laurent Sansonetti authored
7 Fonts/README
View
@@ -0,0 +1,7 @@
+Fonts.app
+
+This sample demonstrates the following concepts: Table View, Accessory View, Fonts.
+
+This sample was contributed by Samuel Chandra.
+
+https://github.com/samchandra/Fonts
8 Fonts/Rakefile
View
@@ -0,0 +1,8 @@
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
+
+Motion::Project::App.setup do |app|
+ # Use `rake config' to see complete project settings.
+ app.name = 'Fonts'
+ app.frameworks += ['QuartzCore']
+end
9 Fonts/app/app_delegate.rb
View
@@ -0,0 +1,9 @@
+class AppDelegate
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
+ @window.rootViewController = UINavigationController.alloc.initWithRootViewController(RootController.alloc.init)
+ @window.rootViewController.wantsFullScreenLayout = true
+ @window.makeKeyAndVisible
+ true
+ end
+end
50 Fonts/app/detail_controller.rb
View
@@ -0,0 +1,50 @@
+class DetailController < UIViewController
+ def loadView
+ self.view = UIScrollView.alloc.initWithFrame(UIScreen.mainScreen.applicationFrame)
+ view.autoresizingMask = UIViewAutoresizingFlexibleHeight
+ view.autoresizesSubviews = true
+ view.backgroundColor = UIColor.whiteColor
+
+ @text_label = UILabel.alloc.init
+ @text_label.numberOfLines = 0
+ view.addSubview(@text_label)
+ end
+
+ def viewDidLoad
+ navigationItem.title = "Sample Text"
+ end
+
+ def viewDidAppear(animated)
+ font = UIFont.fontWithName(@font_name, size:16)
+ frame_size = sample_text.sizeWithFont(font, constrainedToSize:[self.view.bounds.size.width - 20, 1000], lineBreakMode:UILineBreakModeWordWrap)
+ rect = [[10, 10], [frame_size.width, frame_size.height]]
+
+ @text_label.text = sample_text
+ @text_label.frame = rect
+ @text_label.font = font
+
+ view.contentSize = [self.view.frame.size.width, frame_size.height + 20]
+ end
+
+ def selected_font(font_name)
+ @font_name = font_name
+ end
+
+ def sample_text
+ <<EOS
+ #{@font_name}
+
+ RubyMotion offers developers the expressiveness of Ruby with no compromises. It's hard to imagine a more appealing way to build native iOS applications.
+
+ This is AWESOME! With just minimal iOS experience, I was able to get a working app up and running on my phone. Oh, the feeling of power!
+
+ RubyMotion is a dream come true for Rubyists who want to create fast, native iOS apps!
+
+ Now you can develop in your favorite language on your favorite mobile platform. Done right. For real. What are you waiting for?
+
+ With RubyMotion, we can now leverage our Ruby expertise to solve problems in the mobile space. Developing for iOS will never be the same again.
+
+ After years of iOS work, RubyMotion seems like a thousand kittens playing the piano while sliding down a double-rainbow.
+EOS
+ end
+end
65 Fonts/app/root_controller.rb
View
@@ -0,0 +1,65 @@
+class RootController < UITableViewController
+ def viewDidLoad
+ view.dataSource = view.delegate = self
+ navigationItem.title = "Fonts"
+ end
+
+ def numberOfSectionsInTableView(tableView)
+ fonts.count
+ end
+
+ def tableView(tableView, titleForHeaderInSection:section)
+ fonts[section][:family]
+ end
+
+ def sectionIndexTitlesForTableView(tableView)
+ fonts.map do |e|
+ e[:family].slice(0)
+ end.uniq
+ end
+
+ def tableView(tableView, sectionForSectionIndexTitle:title, atIndex:index)
+ fonts.map do |e|
+ e[:family].slice(0)
+ end.index(title)
+ end
+
+ def tableView(tableView, numberOfRowsInSection:section)
+ fonts[section][:fonts].count
+ end
+
+ CELLID = "fonts"
+ def tableView(tableView, cellForRowAtIndexPath:indexPath)
+ cell = tableView.dequeueReusableCellWithIdentifier(CELLID) || begin
+ cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:CELLID)
+ cell.selectionStyle = UITableViewCellSelectionStyleBlue
+ cell
+ end
+
+ font_dict = fonts[indexPath.section]
+ font_name = font_dict[:fonts][indexPath.row]
+ cell.textLabel.font = UIFont.fontWithName(font_name, size:18)
+ cell.textLabel.text = font_name
+ cell
+ end
+
+ def tableView(tableView, didSelectRowAtIndexPath:indexPath)
+ font_dict = fonts[indexPath.section]
+ font_name = font_dict[:fonts][indexPath.row]
+
+ @detail_controller ||= DetailController.alloc.init
+ @detail_controller.selected_font(font_name)
+ self.navigationController.pushViewController(@detail_controller, animated:true)
+
+ tableView.deselectRowAtIndexPath(indexPath, animated:true)
+ end
+
+ def fonts
+ @fonts ||= begin
+ UIFont.familyNames.sort.map do |family|
+ fonts = UIFont.fontNamesForFamilyName(family).sort
+ {:family => family, :fonts => fonts}
+ end
+ end
+ end
+end
0  Fonts/resources/.gitkeep
View
No changes.
9 Fonts/spec/main_spec.rb
View
@@ -0,0 +1,9 @@
+describe "Application 'Fonts'" do
+ before do
+ @app = UIApplication.sharedApplication
+ end
+
+ it "has one window" do
+ @app.windows.size.should == 1
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.