Skip to content

Commit 39c561b

Browse files
committed
Added textAttributes override
1 parent 2e924e2 commit 39c561b

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

ActiveLabel/ActiveLabel.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public protocol ActiveLabelDelegate: class {
4040
didSet { updateTextStorage(parseText: false) }
4141
}
4242

43+
public var textAttributes: [String : AnyObject]?
44+
4345
// MARK: - public methods
4446
public func handleMentionTap(handler: (String) -> ()) {
4547
mentionTapHandler = handler
@@ -237,21 +239,23 @@ public protocol ActiveLabelDelegate: class {
237239

238240
/// add link attribute
239241
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+
249253
for (type, elements) in activeElements {
250254

251255
switch type {
252256
case .Mention: attributes[NSForegroundColorAttributeName] = mentionColor
253257
case .Hashtag: attributes[NSForegroundColorAttributeName] = hashtagColor
254-
case .URL: attributes[NSForegroundColorAttributeName] = URLColor
258+
case .URL: attributes[NSForegroundColorAttributeName] = URLColor
255259
case .None: ()
256260
}
257261

@@ -303,13 +307,19 @@ public protocol ActiveLabelDelegate: class {
303307
guard let selectedElement = selectedElement else {
304308
return
305309
}
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+
308318
if isSelected {
309319
switch selectedElement.element {
310320
case .Mention(_): attributes[NSForegroundColorAttributeName] = mentionSelectedColor ?? mentionColor
311321
case .Hashtag(_): attributes[NSForegroundColorAttributeName] = hashtagSelectedColor ?? hashtagColor
312-
case .URL(_): attributes[NSForegroundColorAttributeName] = URLSelectedColor ?? URLColor
322+
case .URL(_): attributes[NSForegroundColorAttributeName] = URLSelectedColor ?? URLColor
313323
case .None: ()
314324
}
315325
} else {

0 commit comments

Comments
 (0)