updated to use image text attribute
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
97130fc5e4
commit
2b1bff4078
@ -23,10 +23,6 @@ open class TextLinkCaret: Control {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var heightConstraint: NSLayoutConstraint?
|
private var heightConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
private let containerView = UIView().with{
|
|
||||||
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
}
|
|
||||||
|
|
||||||
private var label = Label().with {
|
private var label = Label().with {
|
||||||
$0.typograpicalStyle = TypographicalStyle.BoldBodyLarge
|
$0.typograpicalStyle = TypographicalStyle.BoldBodyLarge
|
||||||
}
|
}
|
||||||
@ -78,25 +74,18 @@ open class TextLinkCaret: Control {
|
|||||||
publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in
|
publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in
|
||||||
self?.sendActions(for: .touchUpInside)
|
self?.sendActions(for: .touchUpInside)
|
||||||
}.store(in: &subscribers)
|
}.store(in: &subscribers)
|
||||||
|
|
||||||
|
|
||||||
addSubview(containerView)
|
|
||||||
containerView.addSubview(label)
|
|
||||||
containerView.addSubview(caretView)
|
|
||||||
|
|
||||||
//constraints
|
//constraints
|
||||||
heightAnchor.constraint(equalToConstant: height).isActive = true
|
heightAnchor.constraint(equalToConstant: height).isActive = true
|
||||||
|
|
||||||
containerView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
let size = caretView.size!.dimensions()
|
||||||
containerView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
caretView.frame = .init(x: 0, y: 0, width: size.width, height: size.height)
|
||||||
containerView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
addSubview(label)
|
||||||
containerView.heightAnchor.constraint(lessThanOrEqualToConstant: height).isActive = true
|
label.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
||||||
|
label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
||||||
label.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
|
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||||
label.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
|
label.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
||||||
label.widthAnchor.constraint(lessThanOrEqualTo: containerView.widthAnchor, multiplier: 0.90).isActive = true
|
|
||||||
caretView.bottomAnchor.constraint(lessThanOrEqualTo: label.bottomAnchor, constant: -3).isActive = true
|
|
||||||
caretView.setConstraints()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -105,25 +94,6 @@ open class TextLinkCaret: Control {
|
|||||||
private var caretLeadingConstraint: NSLayoutConstraint?
|
private var caretLeadingConstraint: NSLayoutConstraint?
|
||||||
private var caretTrailingConstraint: NSLayoutConstraint?
|
private var caretTrailingConstraint: NSLayoutConstraint?
|
||||||
private var labelConstraint: NSLayoutConstraint?
|
private var labelConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
private func setConstraints(){
|
|
||||||
caretLeadingConstraint?.isActive = false
|
|
||||||
caretTrailingConstraint?.isActive = false
|
|
||||||
labelConstraint?.isActive = false
|
|
||||||
if iconPosition == .right {
|
|
||||||
labelConstraint = label.leadingAnchor.constraint(equalTo: containerView.leadingAnchor)
|
|
||||||
caretLeadingConstraint = caretView.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: 4)
|
|
||||||
caretTrailingConstraint = caretView.trailingAnchor.constraint(lessThanOrEqualTo: containerView.trailingAnchor)
|
|
||||||
} else {
|
|
||||||
labelConstraint = label.trailingAnchor.constraint(lessThanOrEqualTo: containerView.trailingAnchor)
|
|
||||||
caretLeadingConstraint = caretView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor)
|
|
||||||
caretTrailingConstraint = caretView.trailingAnchor.constraint(equalTo: label.leadingAnchor, constant: -4)
|
|
||||||
}
|
|
||||||
caretTrailingConstraint?.isActive = true
|
|
||||||
caretLeadingConstraint?.isActive = true
|
|
||||||
labelConstraint?.isActive = true
|
|
||||||
self.layoutIfNeeded()
|
|
||||||
}
|
|
||||||
|
|
||||||
open override func reset() {
|
open override func reset() {
|
||||||
super.reset()
|
super.reset()
|
||||||
@ -135,16 +105,37 @@ open class TextLinkCaret: Control {
|
|||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open override func updateView() {
|
open override func updateView() {
|
||||||
label.surface = surface
|
|
||||||
label.disabled = disabled
|
|
||||||
label.text = text
|
|
||||||
|
|
||||||
|
var updatedText = text ?? ""
|
||||||
|
|
||||||
caretView.surface = surface
|
caretView.surface = surface
|
||||||
caretView.disabled = disabled
|
caretView.disabled = disabled
|
||||||
caretView.direction = iconPosition == .right ? CaretView.Direction.right : CaretView.Direction.left
|
caretView.direction = iconPosition == .right ? CaretView.Direction.right : CaretView.Direction.left
|
||||||
setConstraints()
|
|
||||||
}
|
let image = caretView.getImage()
|
||||||
|
let location = iconPosition == .right ? updatedText.count + 1 : 0
|
||||||
|
let textColor = label.textColorConfiguration.getColor(self)
|
||||||
|
let imageAttribute = ImageLabelAttribute(location: location,
|
||||||
|
length: 1,
|
||||||
|
image: image,
|
||||||
|
frame: .init(x: 0, y: 0, width: image.size.width, height: image.size.height),
|
||||||
|
tintColor: textColor)
|
||||||
|
label.surface = surface
|
||||||
|
label.disabled = disabled
|
||||||
|
label.text = iconPosition == .right ? "\(updatedText) " : " \(updatedText)"
|
||||||
|
label.attributes = [imageAttribute]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
extension UIView {
|
||||||
|
public func getImage() -> UIImage {
|
||||||
|
let renderer = UIGraphicsImageRenderer(size: self.bounds.size)
|
||||||
|
let image = renderer.image { ctx in
|
||||||
|
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
|
||||||
|
}
|
||||||
|
return image
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class CaretView: View {
|
internal class CaretView: View {
|
||||||
@ -221,6 +212,12 @@ internal class CaretView: View {
|
|||||||
self.init(frame: .zero)
|
self.init(frame: .zero)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public convenience init(size: CaretSize){
|
||||||
|
let dimensions = size.dimensions()
|
||||||
|
self.init(frame: .init(x: 0, y: 0, width: dimensions.width, height: dimensions.height))
|
||||||
|
self.size = size
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Setup
|
// MARK: - Setup
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|||||||
@ -12,11 +12,13 @@ public struct ImageLabelAttribute: AttachmentLabelAttributeModel {
|
|||||||
public enum Error: Swift.Error {
|
public enum Error: Swift.Error {
|
||||||
case bundleNotFound
|
case bundleNotFound
|
||||||
case imageNotFound(String)
|
case imageNotFound(String)
|
||||||
|
case imageNotSet
|
||||||
}
|
}
|
||||||
public var id = UUID()
|
public var id = UUID()
|
||||||
public var location: Int
|
public var location: Int
|
||||||
public var length: Int
|
public var length: Int
|
||||||
public var imageName: String
|
public var imageName: String?
|
||||||
|
public var image: UIImage?
|
||||||
public var frame: CGRect
|
public var frame: CGRect
|
||||||
public var tintColor: UIColor
|
public var tintColor: UIColor
|
||||||
public static func == (lhs: ImageLabelAttribute, rhs: ImageLabelAttribute) -> Bool {
|
public static func == (lhs: ImageLabelAttribute, rhs: ImageLabelAttribute) -> Bool {
|
||||||
@ -27,18 +29,34 @@ public struct ImageLabelAttribute: AttachmentLabelAttributeModel {
|
|||||||
return id == equatable.id && range == equatable.range && imageName == equatable.imageName
|
return id == equatable.id && range == equatable.range && imageName == equatable.imageName
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getAttachment() throws -> NSTextAttachment {
|
private func imageAttachment(image: UIImage) -> NSTextAttachment {
|
||||||
guard let bundle = Bundle(identifier: "com.vzw.vds") else {
|
|
||||||
throw Error.bundleNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let image = UIImage(named: imageName, in: bundle, with: nil) else {
|
|
||||||
throw Error.imageNotFound(imageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
let attachment = NSTextAttachment()
|
let attachment = NSTextAttachment()
|
||||||
attachment.image = image.withTintColor(tintColor)
|
attachment.image = image.withTintColor(tintColor)
|
||||||
attachment.bounds = frame
|
attachment.bounds = frame
|
||||||
return attachment
|
return attachment
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func getAttachment() throws -> NSTextAttachment {
|
||||||
|
|
||||||
|
//get a local asset
|
||||||
|
if let imageName {
|
||||||
|
guard let bundle = Bundle(identifier: "com.vzw.vds") else {
|
||||||
|
throw Error.bundleNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let image = UIImage(named: imageName, in: bundle, with: nil) else {
|
||||||
|
throw Error.imageNotFound(imageName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return imageAttachment(image: image)
|
||||||
|
|
||||||
|
} //get from set image
|
||||||
|
else if let image {
|
||||||
|
return imageAttachment(image: image)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw Error.imageNotSet
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user