63 lines
1.6 KiB
Swift
63 lines
1.6 KiB
Swift
//
|
|
// HyperlinkTextField.swift
|
|
// Cyberlock
|
|
//
|
|
// Created by Juraldinio on 8/31/19.
|
|
// Copyright © 2019 Omicronmedia. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
protocol HyperlinkTextFieldDelegate: AnyObject {
|
|
func hyperlinkClicked(target: HyperlinkTextField)
|
|
}
|
|
|
|
final class HyperlinkTextField: UITextView, UITextViewDelegate {
|
|
|
|
private enum Constants {
|
|
static let linkColor = UIColor(red: 173 / 255, green: 179 / 255, blue: 210 / 255, alpha: 0.8)
|
|
}
|
|
|
|
weak var hyperlinkDelegate: HyperlinkTextFieldDelegate?
|
|
|
|
// MARK: - Initializers
|
|
|
|
init(frame: CGRect) {
|
|
super.init(frame: frame, textContainer: nil)
|
|
|
|
self.delegate = self
|
|
|
|
self.isEditable = false
|
|
self.backgroundColor = nil
|
|
self.isSelectable = false
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
if let font = self.font {
|
|
self.font = UIFont(name: font.fontName, size: 12.0)
|
|
}
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
func show(hyper link: String, using color: UIColor? = nil) {
|
|
let textAttributes = [
|
|
NSAttributedString.Key.link: "",
|
|
NSAttributedString.Key.foregroundColor: color ?? Constants.linkColor
|
|
] as [NSAttributedString.Key: Any]
|
|
|
|
self.attributedText = NSAttributedString(string: link, attributes: textAttributes)
|
|
}
|
|
|
|
/*override func mouseDown(with event: NSEvent) {
|
|
self.hyperlinkDelegate?.hyperlinkClicked(target: self)
|
|
}*/
|
|
|
|
// MARK: - UITextViewDelegate
|
|
|
|
}
|