|
9 | 9 | import UIKit
|
10 | 10 |
|
11 | 11 | class MarqueeLabelDemoPushController: UIViewController {
|
12 |
| - @IBOutlet weak var demoLabel: MarqueeLabel! |
13 |
| - @IBOutlet weak var labelLabel: UILabel! |
14 |
| - |
15 |
| - override func viewDidLoad() { |
16 |
| - super.viewDidLoad() |
17 |
| - |
18 |
| - demoLabel.type = MarqueeLabel.MarqueeType.allCases.randomElement() ?? .continuous |
19 |
| - // Set label label text |
20 |
| - labelLabel.text = { () -> String in |
21 |
| - switch demoLabel.type { |
22 |
| - case .continuous: |
23 |
| - return "Continuous scrolling" |
24 |
| - case .continuousReverse: |
25 |
| - return "Continuous Reverse scrolling" |
26 |
| - case .left: |
27 |
| - return "Left-Only scrolling" |
28 |
| - case .leftRight: |
29 |
| - return "Left-Right scrolling" |
30 |
| - case .right: |
31 |
| - return "Right-Only scrolling" |
32 |
| - case .rightLeft: |
33 |
| - return "Right-Left scrolling" |
34 |
| - } |
35 |
| - }() |
36 |
| - |
37 |
| - demoLabel.speed = .duration(15) |
38 |
| - demoLabel.animationCurve = .easeInOut |
39 |
| - demoLabel.fadeLength = 10.0 |
40 |
| - demoLabel.leadingBuffer = 30.0 |
41 |
| - |
42 |
| - let strings = ["When shall we three meet again in thunder, lightning, or in rain? When the hurlyburly's done, When the battle 's lost and won.", |
43 |
| - "I have no spur to prick the sides of my intent, but only vaulting ambition, which o'erleaps itself, and falls on the other.", |
44 |
| - "Double, double toil and trouble; Fire burn, and cauldron bubble.", |
45 |
| - "By the pricking of my thumbs, Something wicked this way comes.", |
46 |
| - "My favorite things in life don't cost any money. It's really clear that the most precious resource we all have is time.", |
47 |
| - "Be a yardstick of quality. Some people aren't used to an environment where excellence is expected."] |
48 |
| - |
49 |
| - demoLabel.text = strings[Int(arc4random_uniform(UInt32(strings.count)))] |
50 |
| - |
51 |
| - let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.didTap(_:))) |
52 |
| - tapRecognizer.numberOfTapsRequired = 1 |
53 |
| - tapRecognizer.numberOfTouchesRequired = 1 |
54 |
| - demoLabel.addGestureRecognizer(tapRecognizer) |
55 |
| - demoLabel.isUserInteractionEnabled = true |
56 |
| - } |
57 |
| - |
58 |
| - @objc func didTap(_ recognizer: UIGestureRecognizer) { |
59 |
| - let label = recognizer.view as! MarqueeLabel |
60 |
| - if recognizer.state == .ended { |
61 |
| - label.isPaused ? label.unpauseLabel() : label.pauseLabel() |
62 |
| - // Convert tap points |
63 |
| - let tapPoint = recognizer.location(in: label) |
64 |
| - print("Frame coord: \(tapPoint)") |
65 |
| - guard let textPoint = label.textCoordinateForFramePoint(tapPoint) else { |
66 |
| - return |
67 |
| - } |
68 |
| - print(" Text coord: \(textPoint)") |
69 |
| - |
70 |
| - // Thanks to Toomas Vahter for the basis of the below |
71 |
| - // https://augmentedcode.io/2020/12/20/opening-hyperlinks-in-uilabel-on-ios/ |
72 |
| - // Create layout manager |
73 |
| - let layoutManager = NSLayoutManager() |
74 |
| - let textContainer = NSTextContainer(size: label.textLayoutSize()) |
75 |
| - textContainer.lineFragmentPadding = 0 |
76 |
| - // Create text storage |
77 |
| - guard let text = label.text else { return } |
78 |
| - let textStorage = NSTextStorage(string: "") |
79 |
| - textStorage.setAttributedString(label.attributedText!) |
80 |
| - layoutManager.addTextContainer(textContainer) |
81 |
| - textStorage.addLayoutManager(layoutManager) |
82 |
| - textContainer.lineBreakMode = label.lineBreakMode |
83 |
| - textContainer.size = label.textRect(forBounds: CGRect(origin: .zero, size:label.textLayoutSize()), limitedToNumberOfLines: label.numberOfLines).size |
84 |
| - |
85 |
| - let characterIndex = layoutManager.characterIndex(for: textPoint, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil) |
86 |
| - guard characterIndex >= 0, characterIndex != NSNotFound else { |
87 |
| - print("No character at point found!") |
88 |
| - return |
89 |
| - } |
90 |
| - |
91 |
| - let stringIndex = text.index(text.startIndex, offsetBy: characterIndex) |
92 |
| - // Print character under touch point |
93 |
| - print("Character under touch point: \(text[stringIndex])") |
94 |
| - } |
| 12 | + @IBOutlet weak var demoLabel: MarqueeLabel! |
| 13 | + @IBOutlet weak var labelLabel: UILabel! |
| 14 | + |
| 15 | + override func viewDidLoad() { |
| 16 | + super.viewDidLoad() |
| 17 | + |
| 18 | + demoLabel.type = MarqueeLabel.MarqueeType.allCases.randomElement() ?? .continuous |
| 19 | + // Set label label text |
| 20 | + labelLabel.text = { () -> String in |
| 21 | + switch demoLabel.type { |
| 22 | + case .continuous: |
| 23 | + return "Continuous scrolling" |
| 24 | + case .continuousReverse: |
| 25 | + return "Continuous Reverse scrolling" |
| 26 | + case .left: |
| 27 | + return "Left-Only scrolling" |
| 28 | + case .leftRight: |
| 29 | + return "Left-Right scrolling" |
| 30 | + case .right: |
| 31 | + return "Right-Only scrolling" |
| 32 | + case .rightLeft: |
| 33 | + return "Right-Left scrolling" |
| 34 | + } |
| 35 | + }() |
| 36 | + |
| 37 | + demoLabel.speed = .duration(15) |
| 38 | + demoLabel.animationCurve = .easeInOut |
| 39 | + demoLabel.fadeLength = 10.0 |
| 40 | + demoLabel.leadingBuffer = 30.0 |
| 41 | + |
| 42 | + let strings = [ |
| 43 | + "When shall we three meet again in thunder, lightning, or in rain? When the hurlyburly's done, When the battle 's lost and won.", |
| 44 | + "I have no spur to prick the sides of my intent, but only vaulting ambition, which o'erleaps itself, and falls on the other.", |
| 45 | + "Double, double toil and trouble; Fire burn, and cauldron bubble.", |
| 46 | + "By the pricking of my thumbs, Something wicked this way comes.", |
| 47 | + "My favorite things in life don't cost any money. It's really clear that the most precious resource we all have is time.", |
| 48 | + "Be a yardstick of quality. Some people aren't used to an environment where excellence is expected.", |
| 49 | + ] |
| 50 | + |
| 51 | + demoLabel.text = strings[Int(arc4random_uniform(UInt32(strings.count)))] |
| 52 | + |
| 53 | + let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.didTap(_:))) |
| 54 | + tapRecognizer.numberOfTapsRequired = 1 |
| 55 | + tapRecognizer.numberOfTouchesRequired = 1 |
| 56 | + demoLabel.addGestureRecognizer(tapRecognizer) |
| 57 | + demoLabel.isUserInteractionEnabled = true |
| 58 | + } |
| 59 | + |
| 60 | + @objc func didTap(_ recognizer: UIGestureRecognizer) { |
| 61 | + let label = recognizer.view as! MarqueeLabel |
| 62 | + if recognizer.state == .ended { |
| 63 | + label.isPaused ? label.unpauseLabel() : label.pauseLabel() |
| 64 | + // Convert tap points |
| 65 | + let tapPoint = recognizer.location(in: label) |
| 66 | + print("Frame coord: \(tapPoint)") |
| 67 | + guard let textPoint = label.textCoordinateForFramePoint(tapPoint) else { |
| 68 | + return |
| 69 | + } |
| 70 | + print(" Text coord: \(textPoint)") |
| 71 | + |
| 72 | + // Thanks to Toomas Vahter for the basis of the below |
| 73 | + // https://augmentedcode.io/2020/12/20/opening-hyperlinks-in-uilabel-on-ios/ |
| 74 | + // Create layout manager |
| 75 | + let layoutManager = NSLayoutManager() |
| 76 | + let textContainer = NSTextContainer(size: label.textLayoutSize()) |
| 77 | + textContainer.lineFragmentPadding = 0 |
| 78 | + // Create text storage |
| 79 | + guard let text = label.text else { return } |
| 80 | + let textStorage = NSTextStorage(string: "") |
| 81 | + textStorage.setAttributedString(label.attributedText!) |
| 82 | + layoutManager.addTextContainer(textContainer) |
| 83 | + textStorage.addLayoutManager(layoutManager) |
| 84 | + textContainer.lineBreakMode = label.lineBreakMode |
| 85 | + textContainer.size = |
| 86 | + label.textRect( |
| 87 | + forBounds: CGRect(origin: .zero, size: label.textLayoutSize()), |
| 88 | + limitedToNumberOfLines: label.numberOfLines |
| 89 | + ).size |
| 90 | + |
| 91 | + let characterIndex = layoutManager.characterIndex( |
| 92 | + for: textPoint, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil) |
| 93 | + guard characterIndex >= 0, characterIndex != NSNotFound else { |
| 94 | + print("No character at point found!") |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + let stringIndex = text.index(text.startIndex, offsetBy: characterIndex) |
| 99 | + // Print character under touch point |
| 100 | + print("Character under touch point: \(text[stringIndex])") |
95 | 101 | }
|
96 |
| - |
| 102 | + } |
| 103 | + |
97 | 104 | }
|
0 commit comments