Class: UILabel
- Defined in:
- lib/ios/sugarcube-to_s/uilabel.rb,
lib/ios/sugarcube-uikit/uilabel.rb,
lib/ios/sugarcube-factories/uilabel.rb
Class Method Summary collapse
-
.new(text = nil, font = nil, size = nil) ⇒ Object
UILabel.new(‘test’) UILabel.new(‘test’, another_label.font) UILabel.new(‘test’, ‘Helvetica’) UILabel.new(‘test’, ‘Helvetica’, 20).
Instance Method Summary collapse
-
#fit_to_size(max_size) ⇒ Object
forces the given text to fit inside the label’s frame starting at a given font size.
- #sugarcube_to_s(options = {}) ⇒ Object
Class Method Details
.new(text = nil, font = nil, size = nil) ⇒ Object
UILabel.new(‘test’) UILabel.new(‘test’, another_label.font) UILabel.new(‘test’, ‘Helvetica’) UILabel.new(‘test’, ‘Helvetica’, 20)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ios/sugarcube-factories/uilabel.rb', line 7 def self.new(text=nil, font=nil, size=nil) return super() if text.nil? font = font.uifont(size) if font.respond_to?(:uifont) label = self.alloc.initWithFrame([[0, 0], [0, 0]]) if text.is_a?(NSAttributedString) label.attributedText = text else label.text = text end if font label.font = font end label.backgroundColor = :clear.uicolor label.sizeToFit label end |
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 = NSLineBreakByWordWrapping 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: NSLineBreakByWordWrapping) 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(={}) text = self.text if text && text.length > 20 text = text[0..20] + '...' end super .merge(inner: {text: text}) end |