Class: EnchantedQuill::Label

Inherits:
UILabel
  • Object
show all
Defined in:
lib/enchanted_quill/label.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#category_colorObject

Returns the value of attribute category_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def category_color
  @category_color
end

#category_selected_colorObject

Returns the value of attribute category_selected_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def category_selected_color
  @category_selected_color
end

#hashtag_colorObject

Returns the value of attribute hashtag_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def hashtag_color
  @hashtag_color
end

#hashtag_selected_colorObject

Returns the value of attribute hashtag_selected_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def hashtag_selected_color
  @hashtag_selected_color
end

#line_height_multipleObject

Returns the value of attribute line_height_multiple.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def line_height_multiple
  @line_height_multiple
end

#line_spacingObject

Returns the value of attribute line_spacing.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def line_spacing
  @line_spacing
end

#maximum_line_heightObject

Returns the value of attribute maximum_line_height.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def maximum_line_height
  @maximum_line_height
end

#mention_colorObject

Returns the value of attribute mention_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def mention_color
  @mention_color
end

#mention_selected_colorObject

Returns the value of attribute mention_selected_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def mention_selected_color
  @mention_selected_color
end

#minimum_line_heightObject

Returns the value of attribute minimum_line_height.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def minimum_line_height
  @minimum_line_height
end

#url_colorObject

Returns the value of attribute url_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def url_color
  @url_color
end

#url_selected_colorObject

Returns the value of attribute url_selected_color.



3
4
5
# File 'lib/enchanted_quill/label.rb', line 3

def url_selected_color
  @url_selected_color
end

Instance Method Details

#attributedText=(attributed_text) ⇒ Object



184
185
186
187
# File 'lib/enchanted_quill/label.rb', line 184

def attributedText=(attributed_text)
  super
  update_text_storage
end

#awakeFromNibObject



22
23
24
25
26
# File 'lib/enchanted_quill/label.rb', line 22

def awakeFromNib
  super.tap do
    update_text_storage
  end
end

#customize(&block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/enchanted_quill/label.rb', line 96

def customize(&block)
  @customizing = true
  block.call(self)
  @customizing = false
  update_text_storage
  self
end

#delegateObject



158
159
160
# File 'lib/enchanted_quill/label.rb', line 158

def delegate
  @delegate
end

#delegate=(delegate) ⇒ Object



154
155
156
# File 'lib/enchanted_quill/label.rb', line 154

def delegate=(delegate)
  @delegate = WeakRef.new(delegate)
end

#drawTextInRect(rect) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/enchanted_quill/label.rb', line 28

def drawTextInRect(rect)
  range               = NSMakeRange(0, text_storage.length)
  text_container.size = rect.size
  new_origin          = text_origin(rect)

  layout_manager.drawBackgroundForGlyphRange(range, atPoint: new_origin)
  layout_manager.drawGlyphsForGlyphRange(range, atPoint: new_origin)
end

#font=(font) ⇒ Object



189
190
191
192
# File 'lib/enchanted_quill/label.rb', line 189

def font=(font)
  super
  update_text_storage(false)
end

#handle_category_tap(&block) ⇒ Object



174
175
176
# File 'lib/enchanted_quill/label.rb', line 174

def handle_category_tap(&block)
  @category_tap_handler = block
end

#handle_hashtag_tap(&block) ⇒ Object



166
167
168
# File 'lib/enchanted_quill/label.rb', line 166

def handle_hashtag_tap(&block)
  @hashtag_tap_handler = block
end

#handle_mention_tap(&block) ⇒ Object



162
163
164
# File 'lib/enchanted_quill/label.rb', line 162

def handle_mention_tap(&block)
  @mention_tap_handler = block
end

#handle_url_tap(&block) ⇒ Object



170
171
172
# File 'lib/enchanted_quill/label.rb', line 170

def handle_url_tap(&block)
  @url_tap_handler = block
end

#initObject



10
11
12
13
14
# File 'lib/enchanted_quill/label.rb', line 10

def init
  super.tap do |label|
    label.setup_label
  end
end

#initWithFrame(frame) ⇒ Object



16
17
18
19
20
# File 'lib/enchanted_quill/label.rb', line 16

def initWithFrame(frame)
  super.tap do |label|
    label.setup_label
  end
end

#layoutSubviewsObject



37
38
39
40
# File 'lib/enchanted_quill/label.rb', line 37

def layoutSubviews
  super
  text_container.size = self.bounds.size
end

#on_touch(touch) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/enchanted_quill/label.rb', line 104

def on_touch(touch)
  location = touch.locationInView(self)
  avoid_super_call = false

  case touch.phase
  when UITouchPhaseBegan, UITouchPhaseMoved
    if element = element_at_location(location)
      if element.range.location != (selected_element && selected_element.range.location) ||
         element.range.length != (selected_element && selected_element.range.length)

         update_attributed_when_selected(false)
         @selected_element = element
         update_attributed_when_selected(true)
       end
       avoid_super_call = true
    else
      update_attributed_when_selected(false)
      @selected_element = nil
    end
  when UITouchPhaseEnded
    return avoid_super_call unless selected_element

    case selected_element.type
    when :category then did_tap_category(selected_element.word)
    when :mention  then did_tap_mention(selected_element.word)
    when :hashtag  then did_tap_hashtag(selected_element.word)
    when :url      then did_tap_string_url(selected_element.word)
    else
    end

    Dispatch::Queue.concurrent.after(0.1) do
      Dispatch::Queue.main.async do
        update_attributed_when_selected(false)
        @selected_element = nil
      end
    end
    avoid_super_call = true
  when UITouchPhaseCancelled
    Dispatch::Queue.concurrent.after(0.1) do
      Dispatch::Queue.main.async do
        update_attributed_when_selected(false)
        @selected_element = nil
      end
    end
  when UITouchPhaseStationary
  end

  avoid_super_call
end

#rect_fitting_text_for_container_size(size, for_number_of_line: num_of_lines) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/enchanted_quill/label.rb', line 72

def rect_fitting_text_for_container_size(size, for_number_of_line: num_of_lines)
  text_container.size = size;
  text_container.maximumNumberOfLines = num_of_lines
  text_bounds = layout_manager.boundingRectForGlyphRange(NSMakeRange(0, layout_manager.numberOfGlyphs),
                  inTextContainer: text_container)
  total_lines = text_bounds.size.height / self.font.lineHeight

  height = text_bounds.size.height
  if num_of_lines > 0 && (num_of_lines < total_lines)
    height -= (total_lines - num_of_lines) * self.font.lineHeight
  elsif (num_of_lines > 0 && (num_of_lines > total_lines))
    height += (num_of_lines - total_lines) * self.font.lineHeight
  end

  width  = text_bounds.size.width.ceil
  height = (attributedText && attributedText.mutableString.blank?) ? 0 : height.ceil
  CGRectMake(text_bounds.origin.x, text_bounds.origin.y, width, height)
end

#requiresConstraintBasedLayoutObject

Override UIView methods



92
93
94
# File 'lib/enchanted_quill/label.rb', line 92

def requiresConstraintBasedLayout
  true
end

#setBounds(bounds) ⇒ Object



52
53
54
55
# File 'lib/enchanted_quill/label.rb', line 52

def setBounds(bounds)
  super
  update_text_container_size(bounds.size)
end

#setFrame(frame) ⇒ Object



47
48
49
50
# File 'lib/enchanted_quill/label.rb', line 47

def setFrame(frame)
  super
  update_text_container_size(frame.size)
end

#setPreferredMaxLayoutWidth(preferred_max_layout_width) ⇒ Object



42
43
44
45
# File 'lib/enchanted_quill/label.rb', line 42

def setPreferredMaxLayoutWidth(preferred_max_layout_width)
  super
  update_text_container_size(self.bounds.size)
end

#setup_labelObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/enchanted_quill/label.rb', line 288

def setup_label
  @setup_label ||= begin
    @customizing = false
    @active_elements = {}
    @height_correction = 0
    self.line_height_multiple    = 1
    self.textColor               = UIColor.blackColor
    self.mention_color           = UIColor.blueColor
    self.mention_selected_color  = UIColor.blueColor.colorWithAlphaComponent(0.5)
    self.hashtag_color           = UIColor.blueColor
    self.hashtag_selected_color  = UIColor.blueColor.colorWithAlphaComponent(0.5)
    self.category_color          = UIColor.brownColor
    self.category_selected_color = UIColor.brownColor.colorWithAlphaComponent(0.5)
    self.url_color               = UIColor.blueColor
    self.url_selected_color      = UIColor.blueColor.colorWithAlphaComponent(0.5)

    text_storage.addLayoutManager(layout_manager)
    layout_manager.addTextContainer(text_container)
    text_container.lineFragmentPadding = 0
    text_container.maximumNumberOfLines = 0
    self.userInteractionEnabled = true
  end
end

#text=(text) ⇒ Object

override UILabel attributes



179
180
181
182
# File 'lib/enchanted_quill/label.rb', line 179

def text=(text)
  super
  update_text_storage
end

#textAlignment=(alignment) ⇒ Object



199
200
201
202
# File 'lib/enchanted_quill/label.rb', line 199

def textAlignment=(alignment)
  super
  update_text_storage(false)
end

#textColor=(color) ⇒ Object



194
195
196
197
# File 'lib/enchanted_quill/label.rb', line 194

def textColor=(color)
  super
  update_text_storage(false)
end

#textRectForBounds(bounds, limitedToNumberOfLines: num_of_lines) ⇒ Object

Override UILabel Methods



66
67
68
69
70
# File 'lib/enchanted_quill/label.rb', line 66

def textRectForBounds(bounds, limitedToNumberOfLines: num_of_lines)
 required_rect = rect_fitting_text_for_container_size(bounds.size, for_number_of_line: num_of_lines)
 text_container.size = required_rect.size
 required_rect
end

#touchesBegan(touches, withEvent: event) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/enchanted_quill/label.rb', line 264

def touchesBegan(touches, withEvent: event)
  touches = touches && touches.allObjects
  touch   = touches.first
  return unless touch
  return if on_touch(touch)
  super
end

#touchesCancelled(touches, withEvent: event) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/enchanted_quill/label.rb', line 272

def touchesCancelled(touches, withEvent: event)
  touches = touches && touches.allObjects
  touch   = touches.first
  return unless touch
  on_touch(touch)
  super
end

#touchesEnded(touches, withEvent: event) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/enchanted_quill/label.rb', line 280

def touchesEnded(touches, withEvent: event)
  touches = touches && touches.allObjects
  touch   = touches.first
  return unless touch
  return if on_touch(touch)
  super
end

#update_text_container_size(size) ⇒ Object



57
58
59
60
61
62
# File 'lib/enchanted_quill/label.rb', line 57

def update_text_container_size(size)
 container_size = size
 width  = [size.width, self.preferredMaxLayoutWidth].min
  height = 0
 text_container.size = CGSizeMake(width, height)
end