Created
February 8, 2019 09:22
-
-
Save trilliwon/8e618db719b0a4f2dccbd009d5958747 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| class TailTagView: UIView { | |
| var rawText: String? { | |
| didSet { | |
| label.attributedText = parseText(rawText: rawText) | |
| } | |
| } | |
| func parseText(rawText: String?, token: String = "**") -> NSAttributedString? { | |
| guard let rawText = rawText, let font = UIFont(name: "AppleSDGothicNeo-Regular", size: 14.0) else { return nil } | |
| let words = rawText.components(separatedBy: "**") | |
| let attributedString = NSMutableAttributedString(string: words.reduce("", +), attributes: [.font: font, .foregroundColor: UIColor.white]) | |
| var location = 0 | |
| print(words) | |
| words.enumerated() | |
| .compactMap { | |
| if $1.isEmpty { return nil } | |
| let range = $0 % 2 == 0 ? nil : NSRange(location: location, length: $1.count) | |
| location += $1.count | |
| return range | |
| } | |
| .forEach { attributedString.addAttribute(.foregroundColor, value: highlightColor, range: $0) } | |
| return attributedString | |
| } | |
| private let highlightColor = UIColor(red: 254.0 / 255.0, green: 181.0 / 255.0, blue: 0, alpha: 1) | |
| private let triangle = TriangleView() | |
| private let contentView = UIView() | |
| private let label = UILabel() | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| configureSubviews() | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| configureSubviews() | |
| } | |
| convenience init() { | |
| self.init(frame: .zero) | |
| } | |
| private func configureSubviews() { | |
| backgroundColor = .clear | |
| translatesAutoresizingMaskIntoConstraints = false | |
| triangle.backgroundColor = .clear | |
| contentView.backgroundColor = UIColor.black.withAlphaComponent(0.6) | |
| contentView.layer.cornerRadius = 2 | |
| contentView.clipsToBounds = true | |
| contentView.isUserInteractionEnabled = true | |
| label.textAlignment = .center | |
| label.sizeToFit() | |
| contentView.translatesAutoresizingMaskIntoConstraints = false | |
| triangle.translatesAutoresizingMaskIntoConstraints = false | |
| label.translatesAutoresizingMaskIntoConstraints = false | |
| addSubview(triangle) | |
| addSubview(contentView) | |
| contentView.addSubview(label) | |
| triangle.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
| triangle.topAnchor.constraint(equalTo: topAnchor).isActive = true | |
| triangle.heightAnchor.constraint(equalToConstant: 10).isActive = true | |
| triangle.widthAnchor.constraint(equalToConstant: 15).isActive = true | |
| contentView.topAnchor.constraint(equalTo: triangle.bottomAnchor).isActive = true | |
| contentView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | |
| contentView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true | |
| contentView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true | |
| label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 9).isActive = true | |
| label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -9).isActive = true | |
| label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true | |
| label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 15).isActive = true | |
| label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -15).isActive = true | |
| contentView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(contentViewTapped))) | |
| } | |
| func show(superView: UIView, toBottomView: UIView) { | |
| self.removeFromSuperview() | |
| superView.addSubview(self) | |
| self.topAnchor.constraint(equalTo: toBottomView.bottomAnchor, constant: 10).isActive = true | |
| self.centerXAnchor.constraint(equalTo: toBottomView.centerXAnchor).isActive = true | |
| self.leadingAnchor.constraint(greaterThanOrEqualTo: superView.leadingAnchor, constant: 15).isActive = true | |
| } | |
| func hide() { | |
| removeFromSuperview() | |
| } | |
| @objc | |
| func contentViewTapped() { | |
| hide() | |
| } | |
| class TriangleView: UIView { | |
| override func draw(_ rect: CGRect) { | |
| guard let context = UIGraphicsGetCurrentContext() else { return } | |
| context.beginPath() | |
| context.move(to: CGPoint(x: rect.midX, y: rect.minY)) | |
| context.addLine(to: CGPoint(x: rect.minX, y: rect.maxY)) | |
| context.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) | |
| context.closePath() | |
| context.setFillColor(UIColor.black.withAlphaComponent(0.6).cgColor) | |
| context.fillPath() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment