-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathExpandableLabel.swift
More file actions
557 lines (481 loc) · 23.6 KB
/
ExpandableLabel.swift
File metadata and controls
557 lines (481 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//
// ExpandableLabel.swift
//
// Copyright (c) 2015 apploft. GmbH
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
typealias LineIndexTuple = (line: CTLine, index: Int)
import UIKit
/**
* The delegate of ExpandableLabel.
*/
@objc public protocol ExpandableLabelDelegate: NSObjectProtocol {
@objc func willExpandLabel(_ label: ExpandableLabel)
@objc func didExpandLabel(_ label: ExpandableLabel)
@objc func willCollapseLabel(_ label: ExpandableLabel)
@objc func didCollapseLabel(_ label: ExpandableLabel)
}
/**
* ExpandableLabel
*/
@objc open class ExpandableLabel: UILabel {
public enum TextReplacementType {
case character
case word
}
/// The delegate of ExpandableLabel
@objc weak open var delegate: ExpandableLabelDelegate?
/// Set 'true' if the label should be collapsed or 'false' for expanded.
@IBInspectable open var collapsed: Bool = true {
didSet {
super.attributedText = (collapsed) ? self.collapsedText : self.expandedText
super.numberOfLines = (collapsed) ? self.collapsedNumberOfLines : 0
if let animationView = animationView {
UIView.animate(withDuration: 0.5) {
animationView.layoutIfNeeded()
}
}
}
}
/// Set 'true' if the label can be expanded or 'false' if not.
/// The default value is 'true'.
@IBInspectable open var shouldExpand: Bool = true
/// Set 'true' if the label can be collapsed or 'false' if not.
/// The default value is 'false'.
@IBInspectable open var shouldCollapse: Bool = false
/// Set the link name (and attributes) that is shown when collapsed.
/// The default value is "More". Cannot be nil.
@objc open var collapsedAttributedLink: NSAttributedString! {
didSet {
self.collapsedAttributedLink = collapsedAttributedLink.copyWithAddedFontAttribute(font)
}
}
/// Set the link name (and attributes) that is shown when expanded.
/// The default value is "Less". Can be nil.
@objc open var expandedAttributedLink: NSAttributedString?
/// Set the ellipsis that appears just after the text and before the link.
/// The default value is "...". Can be nil.
@objc open var ellipsis: NSAttributedString? {
didSet {
self.ellipsis = ellipsis?.copyWithAddedFontAttribute(font)
}
}
/// Set a view to animate changes of the label collapsed state with. If this value is nil, no animation occurs.
/// Usually you assign the superview of this label or a UIScrollView in which this label sits.
/// Also don't forget to set the contentMode of this label to top to smoothly reveal the hidden lines.
/// The default value is 'nil'.
@objc open var animationView: UIView?
open var textReplacementType: TextReplacementType = .word
private var collapsedText: NSAttributedString?
private var linkHighlighted: Bool = false
private let touchSize = CGSize(width: 44, height: 44)
private var linkRect: CGRect?
private var collapsedNumberOfLines: NSInteger = 0
private var expandedLinkPosition: NSTextAlignment?
private var collapsedLinkTextRange: NSRange?
private var expandedLinkTextRange: NSRange?
open override var numberOfLines: NSInteger {
didSet {
collapsedNumberOfLines = numberOfLines
}
}
@objc public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
@objc public override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
@objc public init() {
super.init(frame: .zero)
}
open override var text: String? {
set(text) {
if let text = text {
self.attributedText = NSAttributedString(string: text)
} else {
self.attributedText = nil
}
}
get {
return self.attributedText?.string
}
}
open private(set) var expandedText: NSAttributedString?
open override var attributedText: NSAttributedString? {
set(attributedText) {
if let attributedText = attributedText?.copyWithAddedFontAttribute(font).copyWithParagraphAttribute(font),
attributedText.length > 0 {
self.collapsedText = getCollapsedText(for: attributedText, link: (linkHighlighted) ? collapsedAttributedLink.copyWithHighlightedColor() : self.collapsedAttributedLink)
self.expandedText = getExpandedText(for: attributedText, link: (linkHighlighted) ? expandedAttributedLink?.copyWithHighlightedColor() : self.expandedAttributedLink)
super.attributedText = (self.collapsed) ? self.collapsedText : self.expandedText
} else {
self.expandedText = nil
self.collapsedText = nil
super.attributedText = nil
}
}
get {
return super.attributedText
}
}
open func setLessLinkWith(lessLink: String, attributes: [NSAttributedString.Key: AnyObject], position: NSTextAlignment?) {
var alignedattributes = attributes
if let pos = position {
expandedLinkPosition = pos
let titleParagraphStyle = NSMutableParagraphStyle()
titleParagraphStyle.alignment = pos
alignedattributes[.paragraphStyle] = titleParagraphStyle
}
expandedAttributedLink = NSMutableAttributedString(string: lessLink,
attributes: alignedattributes)
}
}
// MARK: - Touch Handling
extension ExpandableLabel {
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
setLinkHighlighted(touches, event: event, highlighted: true)
}
open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
setLinkHighlighted(touches, event: event, highlighted: false)
}
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
if !collapsed {
guard let range = self.expandedLinkTextRange else {
return
}
if shouldCollapse && check(touch: touch, isInRange: range) {
delegate?.willCollapseLabel(self)
collapsed = true
delegate?.didCollapseLabel(self)
linkHighlighted = isHighlighted
setNeedsDisplay()
}
} else {
if shouldExpand && setLinkHighlighted(touches, event: event, highlighted: false) {
delegate?.willExpandLabel(self)
collapsed = false
delegate?.didExpandLabel(self)
}
}
}
open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
setLinkHighlighted(touches, event: event, highlighted: false)
}
}
// MARK: Privates
extension ExpandableLabel {
private func commonInit() {
isUserInteractionEnabled = true
lineBreakMode = .byClipping
collapsedNumberOfLines = numberOfLines
expandedAttributedLink = nil
collapsedAttributedLink = NSAttributedString(string: "More", attributes: [.font: UIFont.boldSystemFont(ofSize: font.pointSize)])
ellipsis = NSAttributedString(string: "...")
}
private func textReplaceWordWithLink(_ lineIndex: LineIndexTuple, text: NSAttributedString, linkName: NSAttributedString) -> NSAttributedString {
let lineText = text.text(for: lineIndex.line)
var lineTextWithLink = lineText
(lineText.string as NSString).enumerateSubstrings(in: NSRange(location: 0, length: lineText.length), options: [.byWords, .reverse]) { (word, subRange, enclosingRange, stop) -> Void in
let lineTextWithLastWordRemoved = lineText.attributedSubstring(from: NSRange(location: 0, length: subRange.location))
let lineTextWithAddedLink = NSMutableAttributedString(attributedString: lineTextWithLastWordRemoved)
if let ellipsis = self.ellipsis {
lineTextWithAddedLink.append(ellipsis)
lineTextWithAddedLink.append(NSAttributedString(string: " ",
attributes: [.font: (self.font ?? UIFont.preferredFont(forTextStyle: .body))]))
}
lineTextWithAddedLink.append(linkName)
let fits = self.textFitsWidth(lineTextWithAddedLink)
if fits {
lineTextWithLink = lineTextWithAddedLink
let lineTextWithLastWordRemovedRect = lineTextWithLastWordRemoved.boundingRect(for: self.frame.size.width)
let wordRect = linkName.boundingRect(for: self.frame.size.width)
let width = lineTextWithLastWordRemoved.string == "" ? self.frame.width : wordRect.size.width
self.linkRect = CGRect(x: lineTextWithLastWordRemovedRect.size.width, y: self.font.lineHeight * CGFloat(lineIndex.index), width: width, height: wordRect.size.height)
stop.pointee = true
}
}
return lineTextWithLink
}
private func textReplaceWithLink(_ lineIndex: LineIndexTuple, text: NSAttributedString, linkName: NSAttributedString) -> NSAttributedString {
let lineText = text.text(for: lineIndex.line)
let lineTextTrimmedNewLines = NSMutableAttributedString()
lineTextTrimmedNewLines.append(lineText)
let nsString = lineTextTrimmedNewLines.string as NSString
let range = nsString.rangeOfCharacter(from: CharacterSet.newlines)
if range.length > 0 {
lineTextTrimmedNewLines.replaceCharacters(in: range, with: "")
}
let linkText = NSMutableAttributedString()
if let ellipsis = self.ellipsis {
linkText.append(ellipsis)
linkText.append(NSAttributedString(string: " ",
attributes: [.font: (self.font ?? UIFont.preferredFont(forTextStyle: .body))]))
}
linkText.append(linkName)
let lengthDifference = lineTextTrimmedNewLines.string.composedCount - linkText.string.composedCount
let truncatedString = lineTextTrimmedNewLines.attributedSubstring(
from: NSMakeRange(0, lengthDifference >= 0 ? lengthDifference : lineTextTrimmedNewLines.string.composedCount))
let lineTextWithLink = NSMutableAttributedString(attributedString: truncatedString)
lineTextWithLink.append(linkText)
return lineTextWithLink
}
private func getExpandedText(for text: NSAttributedString?, link: NSAttributedString?) -> NSAttributedString? {
guard let text = text else { return nil }
let expandedText = NSMutableAttributedString()
expandedText.append(text)
if let link = link, textWillBeTruncated(expandedText) {
let spaceOrNewLine = expandedLinkPosition == nil ? " " : "\n"
expandedText.append(NSAttributedString(string: "\(spaceOrNewLine)"))
expandedText.append(NSMutableAttributedString(string: "\(link.string)", attributes: link.attributes(at: 0, effectiveRange: nil)).copyWithAddedFontAttribute(font))
expandedLinkTextRange = NSMakeRange(expandedText.length - link.length, link.length)
}
return expandedText
}
private func getCollapsedText(for text: NSAttributedString?, link: NSAttributedString) -> NSAttributedString? {
guard let text = text else { return nil }
let lines = text.lines(for: frame.size.width)
if collapsedNumberOfLines > 0 && collapsedNumberOfLines < lines.count {
let lastLineRef = lines[collapsedNumberOfLines-1] as CTLine
var lineIndex: LineIndexTuple?
var modifiedLastLineText: NSAttributedString?
if self.textReplacementType == .word {
lineIndex = findLineWithWords(lastLine: lastLineRef, text: text, lines: lines)
if let lineIndex = lineIndex {
modifiedLastLineText = textReplaceWordWithLink(lineIndex, text: text, linkName: link)
}
} else {
lineIndex = (lastLineRef, collapsedNumberOfLines - 1)
if let lineIndex = lineIndex {
modifiedLastLineText = textReplaceWithLink(lineIndex, text: text, linkName: link)
}
}
if let lineIndex = lineIndex, let modifiedLastLineText = modifiedLastLineText {
let collapsedLines = NSMutableAttributedString()
for index in 0..<lineIndex.index {
collapsedLines.append(text.text(for:lines[index]))
}
collapsedLines.append(modifiedLastLineText)
collapsedLinkTextRange = NSRange(location: collapsedLines.length - link.length, length: link.length)
return collapsedLines
} else {
return nil
}
}
return text
}
private func findLineWithWords(lastLine: CTLine, text: NSAttributedString, lines: [CTLine]) -> LineIndexTuple {
var lastLineRef = lastLine
var lastLineIndex = collapsedNumberOfLines - 1
var lineWords = spiltIntoWords(str: text.text(for: lastLineRef).string as NSString)
while lineWords.count < 2 && lastLineIndex > 0 {
lastLineIndex -= 1
lastLineRef = lines[lastLineIndex] as CTLine
lineWords = spiltIntoWords(str: text.text(for: lastLineRef).string as NSString)
}
return (lastLineRef, lastLineIndex)
}
private func spiltIntoWords(str: NSString) -> [String] {
var strings: [String] = []
str.enumerateSubstrings(in: NSRange(location: 0, length: str.length), options: [.byWords, .reverse]) { (word, subRange, enclosingRange, stop) -> Void in
if let unwrappedWord = word {
strings.append(unwrappedWord)
}
if strings.count > 1 { stop.pointee = true }
}
return strings
}
private func textFitsWidth(_ text: NSAttributedString) -> Bool {
return (text.boundingRect(for: frame.size.width).size.height <= font.lineHeight) as Bool
}
private func textWillBeTruncated(_ text: NSAttributedString) -> Bool {
let lines = text.lines(for: frame.size.width)
return collapsedNumberOfLines > 0 && collapsedNumberOfLines < lines.count
}
private func textClicked(touches: Set<UITouch>?, event: UIEvent?) -> Bool {
let touch = event?.allTouches?.first
let location = touch?.location(in: self)
let textRect = self.attributedText?.boundingRect(for: self.frame.width)
if let location = location, let textRect = textRect {
let finger = CGRect(x: location.x-touchSize.width/2, y: location.y-touchSize.height/2, width: touchSize.width, height: touchSize.height)
if finger.intersects(textRect) {
return true
}
}
return false
}
@discardableResult private func setLinkHighlighted(_ touches: Set<UITouch>?, event: UIEvent?, highlighted: Bool) -> Bool {
guard let touch = touches?.first else {
return false
}
guard let range = self.collapsedLinkTextRange else {
return false
}
if collapsed && check(touch: touch, isInRange: range) {
linkHighlighted = highlighted
setNeedsDisplay()
return true
}
return false
}
}
// MARK: Convenience Methods
private extension NSAttributedString {
func hasFontAttribute() -> Bool {
guard !self.string.isEmpty else { return false }
let font = self.attribute(.font, at: 0, effectiveRange: nil) as? UIFont
return font != nil
}
func copyWithParagraphAttribute(_ font: UIFont) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.05
paragraphStyle.alignment = .left
paragraphStyle.lineSpacing = 0.0
paragraphStyle.minimumLineHeight = font.lineHeight
paragraphStyle.maximumLineHeight = font.lineHeight
let copy = NSMutableAttributedString(attributedString: self)
let range = NSRange(location: 0, length: copy.length)
copy.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
copy.addAttribute(.baselineOffset, value: font.pointSize * 0.08, range: range)
return copy
}
func copyWithAddedFontAttribute(_ font: UIFont) -> NSAttributedString {
if !hasFontAttribute() {
let copy = NSMutableAttributedString(attributedString: self)
copy.addAttribute(.font, value: font, range: NSRange(location: 0, length: copy.length))
return copy
}
return self.copy() as! NSAttributedString
}
func copyWithHighlightedColor() -> NSAttributedString {
let alphaComponent = CGFloat(0.5)
let baseColor: UIColor = (self.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor)?.withAlphaComponent(alphaComponent) ??
UIColor.black.withAlphaComponent(alphaComponent)
let highlightedCopy = NSMutableAttributedString(attributedString: self)
let range = NSRange(location: 0, length: highlightedCopy.length)
highlightedCopy.removeAttribute(.foregroundColor, range: range)
highlightedCopy.addAttribute(.foregroundColor, value: baseColor, range: range)
return highlightedCopy
}
func lines(for width: CGFloat) -> [CTLine] {
let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: width, height: .greatestFiniteMagnitude))
let frameSetterRef: CTFramesetter = CTFramesetterCreateWithAttributedString(self as CFAttributedString)
let frameRef: CTFrame = CTFramesetterCreateFrame(frameSetterRef, CFRange(location: 0, length: 0), path.cgPath, nil)
let linesNS: NSArray = CTFrameGetLines(frameRef)
let linesAO: [AnyObject] = linesNS as [AnyObject]
let lines: [CTLine] = linesAO as! [CTLine]
return lines
}
func text(for lineRef: CTLine) -> NSAttributedString {
let lineRangeRef: CFRange = CTLineGetStringRange(lineRef)
let range: NSRange = NSRange(location: lineRangeRef.location, length: lineRangeRef.length)
return self.attributedSubstring(from: range)
}
func boundingRect(for width: CGFloat) -> CGRect {
return self.boundingRect(with: CGSize(width: width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin, context: nil)
}
}
extension String {
var composedCount : Int {
var count = 0
enumerateSubstrings(in: startIndex..<endIndex, options: .byComposedCharacterSequences) { _,_,_,_ in count += 1 }
return count
}
}
extension UILabel {
open func check(touch: UITouch, isInRange targetRange: NSRange) -> Bool {
let touchPoint = touch.location(in: self)
let index = characterIndex(at: touchPoint)
return NSLocationInRange(index, targetRange)
}
private func characterIndex(at touchPoint: CGPoint) -> Int {
guard let attributedString = attributedText else { return NSNotFound }
if !bounds.contains(touchPoint) {
return NSNotFound
}
let textRect = self.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
if !textRect.contains(touchPoint) {
return NSNotFound
}
var point = touchPoint
// Offset tap coordinates by textRect origin to make them relative to the origin of frame
point = CGPoint(x: point.x - textRect.origin.x, y: point.y - textRect.origin.y)
// Convert tap coordinates (start at top left) to CT coordinates (start at bottom left)
point = CGPoint(x: point.x, y: textRect.size.height - point.y)
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, attributedString.length), nil, CGSize(width: textRect.width, height: CGFloat.greatestFiniteMagnitude), nil)
let path = CGMutablePath()
path.addRect(CGRect(x: 0, y: 0, width: suggestedSize.width, height: CGFloat(ceilf(Float(suggestedSize.height)))))
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributedString.length), path, nil)
let lines = CTFrameGetLines(frame)
let linesCount = numberOfLines > 0 ? min(numberOfLines, CFArrayGetCount(lines)) : CFArrayGetCount(lines)
if linesCount == 0 {
return NSNotFound
}
var lineOrigins = [CGPoint](repeating: .zero, count: linesCount)
CTFrameGetLineOrigins(frame, CFRangeMake(0, linesCount), &lineOrigins)
for (idx, lineOrigin) in lineOrigins.enumerated() {
var lineOrigin = lineOrigin
let lineIndex = CFIndex(idx)
let line = unsafeBitCast(CFArrayGetValueAtIndex(lines, lineIndex), to: CTLine.self)
// Get bounding information of line
var ascent: CGFloat = 0.0
var descent: CGFloat = 0.0
var leading: CGFloat = 0.0
let width = CGFloat(CTLineGetTypographicBounds(line, &ascent, &descent, &leading))
let yMin = CGFloat(floor(lineOrigin.y - descent))
let yMax = CGFloat(ceil(lineOrigin.y + ascent))
// Apply penOffset using flushFactor for horizontal alignment to set lineOrigin since this is the horizontal offset from drawFramesetter
let flushFactor = flushFactorForTextAlignment(textAlignment: textAlignment)
let penOffset = CGFloat(CTLineGetPenOffsetForFlush(line, flushFactor, Double(textRect.size.width)))
lineOrigin.x = penOffset
// Check if we've already passed the line
if point.y > yMax {
return NSNotFound
}
// Check if the point is within this line vertically
if point.y >= yMin {
// Check if the point is within this line horizontally
if point.x >= lineOrigin.x && point.x <= lineOrigin.x + width {
// Convert CT coordinates to line-relative coordinates
let relativePoint = CGPoint(x: point.x - lineOrigin.x, y: point.y - lineOrigin.y)
return Int(CTLineGetStringIndexForPosition(line, relativePoint))
}
}
}
return NSNotFound
}
private func flushFactorForTextAlignment(textAlignment: NSTextAlignment) -> CGFloat {
switch textAlignment {
case .center:
return 0.5
case .right:
return 1.0
case .left, .natural, .justified:
return 0.0
@unknown default:
fatalError()
}
}
}