Class: NSString

Inherits:
Object
  • Object
show all
Defined in:
lib/cha_work/sugar/nsstring.rb

Instance Method Summary collapse

Instance Method Details

#cgcolor(alpha = nil) ⇒ Object



20
21
22
# File 'lib/cha_work/sugar/nsstring.rb', line 20

def cgcolor(alpha=nil)
  uicolor(alpha).CGColor
end

#height(hash = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cha_work/sugar/nsstring.rb', line 37

def height(hash={})
  font_size = hash[:size] || 16
  width     = hash[:width] || 320
  font      = UIFont.systemFontOfSize(font_size)
  #size      = self.sizeWithFont(font, constrainedToSize:[width, 10000], lineBreakMode:NSLineBreakByCharWrapping)
  rect      = self.boundingRectWithSize([width, 10000], 
                                         options:NSStringDrawingUsesLineFragmentOrigin, 
                                         attributes:{NSFontAttributeName => font},
                                         context:nil)
  rect.size.height.to_i + 1
end

#nsurlObject



3
4
5
# File 'lib/cha_work/sugar/nsstring.rb', line 3

def nsurl
  @url ||= NSURL.alloc.initWithString(self)
end

#uicolor(alpha = nil) ⇒ UIColor

Returns:

  • (UIColor)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cha_work/sugar/nsstring.rb', line 8

def uicolor(alpha=nil)
  if self[0,1] == '#'
    if self.length == 4
      return (self[1] * 2 + self[2] * 2 + self[3] * 2).to_i(16).uicolor(alpha)
    end
    return self[1..-1].to_i(16).uicolor(alpha)
  end

  img = UIImage.imageNamed(self)
  img && img.uicolor(alpha)
end

#uifont(size = nil) ⇒ UIFont

Returns:

  • (UIFont)


32
33
34
35
# File 'lib/cha_work/sugar/nsstring.rb', line 32

def uifont(size=nil)
  size ||= UIFont.systemFontSize
  UIFont.fontWithName(self, size:size)
end

#uiimageUIImage

Returns:

  • (UIImage)


25
26
27
28
29
# File 'lib/cha_work/sugar/nsstring.rb', line 25

def uiimage
  UIImage.imageNamed(self).tap do |retval|
    NSLog("No image named #{self}") unless retval
  end
end

#uiimageviewUIImageView

Returns:



69
70
71
# File 'lib/cha_work/sugar/nsstring.rb', line 69

def uiimageview
  self.uiimage.uiimageview
end

#uilabel(hash = {}) ⇒ UILabel

Parameters:

  • font (UIFont)

    Optional, defaults to UIFont.systemFontOfSize(UIFont.systemFontSize)

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cha_work/sugar/nsstring.rb', line 51

def uilabel(hash={})
  font_size = hash[:size] || 16
  color= hash[:color].uicolor || UIColor.blackColor
  left = hash[:l] || hash[:left] || 0
  top  = hash[:t] || hash[:top] || 0

  font = UIFont.systemFontOfSize(font_size)
  size = self.sizeWithFont(font)
  label = UILabel.alloc.initWithFrame([[left, top], size])
  label.text = self
  label.font = font
  label.textColor = color
  # why isn't this just the default!?
  label.backgroundColor = :clear.uicolor
  label
end