motion-prime/views/view_styler.rb in motion-prime-0.7.0 vs motion-prime/views/view_styler.rb in motion-prime-0.7.1
- old
+ new
@@ -1,17 +1,19 @@
module MotionPrime
class ViewStyler
include FrameCalculatorMixin
include HasStyles
include HasClassFactory
+ include ElementTextMixin
attr_reader :view, :options
def initialize(view, bounds = CGRectZero, options = {})
@options = Styles.extend_and_normalize_options options
@view = view
prepare_frame_for(bounds) if @options.delete(:calculate_frame)
+ prepare_options!
end
def apply
converted_options = convert_primitives_to_objects(options)
converted_options.each do |key, value|
@@ -39,10 +41,33 @@
mask |= UIViewAutoresizingFlexibleHeight if options[:height_to_fit].nil? && (!options[:top].nil? && !options[:bottom].nil?)
options[:autoresizingMask] = mask
end
end
+ def prepare_options!
+ if options.slice(:html, :line_spacing, :line_height, :underline, :fragment_color).any?
+ text_options = extract_attributed_text_options(options)
+
+ html = text_options.delete(:html)
+ text_options[:text] = html if html
+ options[:attributed_text] = html ? html_string(text_options) : attributed_string(text_options)
+
+ # ios 7 bug fix when text is invisible
+ options[:number_of_lines] = 0 if text_options.slice(:line_height, :line_spacing, :text_alignment, :line_break_mode).any? && options.fetch(:number_of_lines, 1) == 1
+ end
+ end
+
+ def extract_attributed_text_options(options)
+ text_attributes = [
+ :text, :html, :line_spacing, :line_height, :underline, :fragment_color,
+ :text_alignment, :font, :line_break_mode, :number_of_lines
+ ]
+ attributed_text_options = options.slice(*text_attributes)
+ options.except!(*text_attributes)
+ attributed_text_options
+ end
+
def set_option(key, value)
# return if value.nil?
# ignore options
return if key == 'section' && !view.respond_to?(:section=)
return if key == 'size_to_fit' && view.is_a?(UILabel)
@@ -102,46 +127,14 @@
mask_path = UIBezierPath.bezierPathWithRoundedRect(bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius))
mask_layer = CAShapeLayer.layer
mask_layer.frame = bounds
mask_layer.path = mask_path.CGPath
view.mask = mask_layer
- elsif key == 'attributed_text_options'
- attributes = {}
- if value[:html] # TODO: use _text_mixin
- styles = []
- styles << "color: #{options[:text_color].hex};" if options[:text_color]
- styles << "line-height: #{options[:line_height] || (options[:line_spacing].to_f + options[:font].pointSize)}px;"
- styles << "font-family: '#{options[:font].familyName}';"
- styles << "font-size: #{options[:font].pointSize}px;"
-
- html_options = {
- NSDocumentTypeDocumentAttribute => NSHTMLTextDocumentType,
- NSCharacterEncodingDocumentAttribute => NSNumber.numberWithInt(NSUTF8StringEncoding)
- }
- text = "#{value[:html]}<style>* { #{styles.join} }</style>"
- view.attributedText = NSAttributedString.alloc.initWithData(text.dataUsingEncoding(NSUTF8StringEncoding), options: html_options, documentAttributes: nil, error: nil)
+ elsif key == 'attributed_text'
+ if view.is_a?(UIButton)
+ view.setAttributedTitle value, forState: UIControlStateNormal
else
- if line_spacing = value[:line_spacing] || line_height = value[:line_height]
- paragrahStyle = NSMutableParagraphStyle.alloc.init
- line_height ? paragrahStyle.setMinimumLineHeight(line_height) : paragrahStyle.setLineSpacing(line_spacing)
- attributes[NSParagraphStyleAttributeName] = paragrahStyle
- end
-
- attributedString = NSAttributedString.alloc.initWithString(value[:text].to_s, attributes: attributes)
- if underline_range = value[:underline]
- underline_range = [0, value[:text].length] if underline_range === true
- attributedString = NSMutableAttributedString.alloc.initWithAttributedString(attributedString)
- attributedString.addAttributes({NSUnderlineStyleAttributeName => NSUnderlineStyleSingle}, range: underline_range)
- end
- if fragment_color = value[:fragment_color]
- attributedString = NSMutableAttributedString.alloc.initWithAttributedString(attributedString)
- attributedString.addAttributes({NSForegroundColorAttributeName => fragment_color[:color].uicolor}, range: fragment_color[:range])
- end
- if view.is_a?(UIButton)
- view.setAttributedTitle attributedString, forState: UIControlStateNormal
- else
- view.attributedText = attributedString
- end
+ view.attributedText = value
end
elsif key == 'gradient'
gradient = prepare_gradient(value)
view.layer.insertSublayer(gradient, atIndex: 0)
elsif key == 'selection_style' && view.is_a?(UITableViewCell) && value.is_a?(Symbol)
\ No newline at end of file