@@ -40,6 +40,8 @@ public protocol ActiveLabelDelegate: class {
40
40
didSet { updateTextStorage ( parseText: false ) }
41
41
}
42
42
43
+ public var textAttributes : [ String : AnyObject ] ?
44
+
43
45
// MARK: - public methods
44
46
public func handleMentionTap( handler: ( String ) -> ( ) ) {
45
47
mentionTapHandler = handler
@@ -237,21 +239,23 @@ public protocol ActiveLabelDelegate: class {
237
239
238
240
/// add link attribute
239
241
private func addLinkAttribute( mutAttrString: NSMutableAttributedString ) {
240
- var range = NSRange ( location: 0 , length: 0 )
241
- var attributes = mutAttrString. attributesAtIndex ( 0 , effectiveRange: & range)
242
-
243
- attributes [ NSFontAttributeName] = font!
244
- attributes [ NSForegroundColorAttributeName] = textColor
245
- mutAttrString. addAttributes ( attributes, range: range)
246
-
247
- attributes [ NSForegroundColorAttributeName] = mentionColor
248
-
242
+
243
+ var attributes : [ String : AnyObject ]
244
+ if textAttributes != nil {
245
+ attributes = textAttributes!
246
+ } else {
247
+ // If attributes were not provided, pull the attributes from the first character.
248
+ var range = NSRange ( location: 0 , length: 0 )
249
+ attributes = mutAttrString. attributesAtIndex ( 0 , effectiveRange: & range)
250
+ attributes [ NSFontAttributeName] = font!
251
+ }
252
+
249
253
for (type, elements) in activeElements {
250
254
251
255
switch type {
252
256
case . Mention: attributes [ NSForegroundColorAttributeName] = mentionColor
253
257
case . Hashtag: attributes [ NSForegroundColorAttributeName] = hashtagColor
254
- case . URL: attributes [ NSForegroundColorAttributeName] = URLColor
258
+ case . URL: attributes [ NSForegroundColorAttributeName] = URLColor
255
259
case . None: ( )
256
260
}
257
261
@@ -303,13 +307,19 @@ public protocol ActiveLabelDelegate: class {
303
307
guard let selectedElement = selectedElement else {
304
308
return
305
309
}
306
-
307
- var attributes = textStorage. attributesAtIndex ( 0 , effectiveRange: nil )
310
+
311
+ var attributes : [ String : AnyObject ]
312
+ if textAttributes != nil {
313
+ attributes = textAttributes!
314
+ } else {
315
+ attributes = textStorage. attributesAtIndex ( 0 , effectiveRange: nil )
316
+ }
317
+
308
318
if isSelected {
309
319
switch selectedElement. element {
310
320
case . Mention( _) : attributes [ NSForegroundColorAttributeName] = mentionSelectedColor ?? mentionColor
311
321
case . Hashtag( _) : attributes [ NSForegroundColorAttributeName] = hashtagSelectedColor ?? hashtagColor
312
- case . URL( _) : attributes [ NSForegroundColorAttributeName] = URLSelectedColor ?? URLColor
322
+ case . URL( _) : attributes [ NSForegroundColorAttributeName] = URLSelectedColor ?? URLColor
313
323
case . None: ( )
314
324
}
315
325
} else {
0 commit comments