Class: UILabel

Inherits:
Object show all
Defined in:
lib/ios/sugarcube-to_s/uilabel.rb,
lib/ios/sugarcube-uikit/uilabel.rb

Instance Method Summary collapse

Instance Method Details

#fit_to_size(max_size) ⇒ Object

forces the given text to fit inside the label’s frame starting at a given font size



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ios/sugarcube-uikit/uilabel.rb', line 4

def fit_to_size(max_size)
  #enforce word wrap
  self.lineBreakMode = UILineBreakModeWordWrap
  
  dynamic_font = self.font.fontWithSize(max_size + 2)
  constraintSize = CGSizeMake(self.frame.size.width, 10000)

  # does it fit yet?
  begin
    dynamic_font = dynamic_font.fontWithSize(dynamic_font.pointSize - 2)
    current_size = self.text.sizeWithFont(dynamic_font, constrainedToSize:constraintSize, lineBreakMode:UILineBreakModeWordWrap)
  end while self.frame.size.height <= current_size.height

  #now set to font size we have settled on
  self.font = dynamic_font

end

#sugarcube_to_s(options = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/ios/sugarcube-to_s/uilabel.rb', line 3

def sugarcube_to_s(options={})
  text = self.text
  if text && text.length > 20
    text = text[0..20] + '...'
  end
  super options.merge(inner: {text: text})
end