Module: MotionPrime::ElementTextMixin
- Included in:
- ElementContentTextMixin, LabelDrawElement, LabelElement, ViewStyler
- Defined in:
- motion-prime/elements/_text_mixin.rb
Instance Method Summary collapse
- #attributed_string(options) ⇒ Object
- #extract_attributed_string_options(options) ⇒ Object
-
#html_string(options) ⇒ Object
Options text text_color font line_spacing text_alignment text_alignment_name line_break_mode underline (range).
Instance Method Details
#attributed_string(options) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'motion-prime/elements/_text_mixin.rb', line 30 def attributed_string() attributes, paragrah_style = () prepared_text = NSMutableAttributedString.alloc.initWithString([:text].to_s, attributes: attributes) underline_range = [:underline] fragment_color = [:fragment_color] if paragrah_style && (underline_range || fragment_color) && .fetch(:number_of_lines, 1) == 1 Prime.logger.debug "If attributed text has paragraph style and underline - you must set number of lines != 1" end if underline_range underline_range = [0, [:text].length] if underline_range === true prepared_text.addAttributes({NSUnderlineStyleAttributeName => NSUnderlineStyleSingle}, range: underline_range) end if fragment_color prepared_text.addAttributes({NSForegroundColorAttributeName => fragment_color[:color].uicolor}, range: fragment_color[:range]) end prepared_text end |
#extract_attributed_string_options(options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'motion-prime/elements/_text_mixin.rb', line 50 def () attributes = {} line_height = [:line_height] line_spacing = [:line_spacing] text_alignment = [:text_alignment] line_break_mode = [:line_break_mode] if line_height || line_spacing || text_alignment || line_break_mode paragrah_style = NSMutableParagraphStyle.alloc.init if line_height paragrah_style.setMinimumLineHeight(line_height) elsif line_spacing paragrah_style.setLineSpacing(line_spacing) end if text_alignment text_alignment = text_alignment.nstextalignment if text_alignment.is_a?(Symbol) paragrah_style.setAlignment(text_alignment) end if line_break_mode line_break_mode = line_break_mode.uilinebreakmode if line_break_mode.is_a?(Symbol) paragrah_style.setLineBreakMode(line_break_mode) end attributes[NSParagraphStyleAttributeName] = paragrah_style end attributes[NSForegroundColorAttributeName] = [:text_color].uicolor if [:text_color] attributes[NSFontAttributeName] = [:font].uifont if [:font] [attributes, paragrah_style] end |
#html_string(options) ⇒ Object
Options text text_color font line_spacing text_alignment text_alignment_name line_break_mode underline (range)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'motion-prime/elements/_text_mixin.rb', line 13 def html_string() styles = [] styles << "color: #{[:text_color].hex};" if [:text_color] styles << "line-height: #{[:line_height] || ([:line_spacing].to_f + [:font].pointSize)}px;" styles << "font-family: '#{[:font].familyName}';" styles << "font-size: #{[:font].pointSize}px;" styles << "text-align: #{[:text_alignment_name]};" if [:text_alignment_name] = { NSDocumentTypeDocumentAttribute => NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute => NSNumber.numberWithInt(NSUTF8StringEncoding) } text = "#{[:text]}<style>* { #{styles.join} }</style>" NSAttributedString.alloc.initWithData(text.dataUsingEncoding(NSUTF8StringEncoding), options: , documentAttributes: nil, error: nil) end |