Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add bubble-wrap/font

  • Loading branch information...
commit 74a7f4e702e2f0e4fe75a91bc90dd44f9439e7a6 1 parent f0faa53
Clay Allsopp clayallsopp authored
2  lib/bubble-wrap/all.rb
View
@@ -1,4 +1,4 @@
require File.expand_path('../loader', __FILE__)
-['core', 'http', 'reactor', 'rss_parser', 'ui', 'location', 'media'].each { |sub|
+['core', 'http', 'reactor', 'rss_parser', 'ui', 'location', 'media', 'font'].each { |sub|
require File.expand_path("../#{sub}", __FILE__)
}
2  lib/bubble-wrap/font.rb
View
@@ -0,0 +1,2 @@
+require 'bubble-wrap/loader'
+BubbleWrap.require('motion/font/font.rb')
85 motion/font/font.rb
View
@@ -0,0 +1,85 @@
+module BubbleWrap
+ module Font
+ module_function
+
+ def bold(size)
+ Font.new(:bold, size)
+ end
+
+ def system(size)
+ Font.new(:system, size)
+ end
+
+ def italic(size)
+ Font.new(:italic, size)
+ end
+
+ # Example
+ # Font.new(<# UIFont >)
+ # Font.new("Helvetica")
+ # Font.new("Helvetica", 12)
+ # Font.new("Helvetica", size: 12)
+ # Font.new(name: "Helvetica", size: 12)
+ def new(params = {}, *args)
+ if params.is_a?(UIFont)
+ return params
+ end
+ _font = nil
+
+ if params.is_a?(NSString)
+ params = {name: params}
+ end
+
+ if args && !args.empty?
+ case args[0]
+ when NSDictionary
+ params.merge!(args[0])
+ else
+ params.merge!({size: args[0]})
+ end
+ end
+ params[:size] ||= UIFont.systemFontSize
+
+ case params[:name].to_sym
+ when :system
+ _font = UIFont.systemFontOfSize(params[:size].to_f)
+ when :bold
+ _font = UIFont.boldSystemFontOfSize(params[:size].to_f)
+ when :italic
+ _font = UIFont.italicSystemFontOfSize(params[:size].to_f)
+ else
+ begin
+ _font = UIFont.fontWithName(params[:name], size: params[:size])
+ rescue
+ end
+ end
+
+ if !_font
+ raise "Invalid font for parameters: #{params.inspect} args #{args.inspect}"
+ end
+
+ _font
+ end
+
+ class << self
+ alias_method :named, :new
+ end
+
+ # I.e. for UINavigationBar#titleTextAttributes
+ def attributes(params = {})
+ _attributes = {}
+
+ _attributes[UITextAttributeFont] = Font.new(params[:font]) if params[:font]
+ _attributes[UITextAttributeTextColor] = params[:color].to_color if params[:color]
+ _attributes[UITextAttributeTextShadowColor] = params[:shadow_color].to_color if params[:shadow_color]
+ _attributes[UITextAttributeTextShadowOffset] = begin
+ x = params[:shadow_offset][:x]
+ y = params[:shadow_offset][:y]
+ offset = UIOffsetMake(x,y)
+ NSValue.valueWithUIOffset(offset)
+ end if params[:shadow_offset]
+
+ _attributes
+ end
+ end
+end
51 spec/motion/font/font_spec.rb
View
@@ -0,0 +1,51 @@
+describe BubbleWrap::Font do
+ [[:system, "systemFontOfSize:"], [:bold, "boldSystemFontOfSize:"], [:italic, "italicSystemFontOfSize:"]].each do |font, method|
+ describe ".#{font}" do
+ it "should work" do
+ f = BubbleWrap::Font.send(font, 12)
+ f.should == UIFont.send(method, 12)
+ end
+ end
+ end
+
+ describe ".make" do
+ it "should work with UIFont" do
+ BubbleWrap::Font.new(UIFont.boldSystemFontOfSize(12)).should == UIFont.boldSystemFontOfSize(12)
+ end
+
+ it "should work with string" do
+ BubbleWrap::Font.new("Helvetica").should == UIFont.fontWithName("Helvetica", size: UIFont.systemFontSize)
+ end
+
+ it "should work with string and size" do
+ BubbleWrap::Font.new("Helvetica", 18).should == UIFont.fontWithName("Helvetica", size: 18)
+ end
+
+ it "should work with string and hash" do
+ BubbleWrap::Font.new("Helvetica", size: 16).should == UIFont.fontWithName("Helvetica", size: 16)
+ end
+
+ it "should work with hash" do
+ BubbleWrap::Font.new(name: "Chalkduster", size: 16).should == UIFont.fontWithName("Chalkduster", size: 16)
+ end
+ end
+
+ describe ".named" do
+ it "should work" do
+ BubbleWrap::Font.named("Helvetica").should == UIFont.fontWithName("Helvetica", size: UIFont.systemFontSize)
+ end
+ end
+
+ describe ".attributes" do
+ it "should work" do
+ _attributes = BubbleWrap::Font.attributes(font: UIFont.systemFontOfSize(12), color: "red", shadow_color: "blue", shadow_offset: {x: 5, y: 10})
+
+ _attributes.should == {
+ UITextAttributeFont => UIFont.systemFontOfSize(12),
+ UITextAttributeTextColor => UIColor.redColor,
+ UITextAttributeTextShadowColor => UIColor.blueColor,
+ UITextAttributeTextShadowOffset => NSValue.valueWithUIOffset(UIOffsetMake(5, 10))
+ }
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.