updated to use image text attribute

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-11-02 16:01:11 -05:00
parent 97130fc5e4
commit 2b1bff4078
2 changed files with 68 additions and 53 deletions

View File

@ -23,10 +23,6 @@ open class TextLinkCaret: Control {
//--------------------------------------------------
private var heightConstraint: NSLayoutConstraint?
private let containerView = UIView().with{
$0.translatesAutoresizingMaskIntoConstraints = false
}
private var label = Label().with {
$0.typograpicalStyle = TypographicalStyle.BoldBodyLarge
}
@ -78,25 +74,18 @@ open class TextLinkCaret: Control {
publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in
self?.sendActions(for: .touchUpInside)
}.store(in: &subscribers)
addSubview(containerView)
containerView.addSubview(label)
containerView.addSubview(caretView)
//constraints
heightAnchor.constraint(equalToConstant: height).isActive = true
containerView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
containerView.heightAnchor.constraint(lessThanOrEqualToConstant: height).isActive = true
label.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: containerView.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()
let size = caretView.size!.dimensions()
caretView.frame = .init(x: 0, y: 0, width: size.width, height: size.height)
addSubview(label)
label.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
//--------------------------------------------------
@ -105,25 +94,6 @@ open class TextLinkCaret: Control {
private var caretLeadingConstraint: NSLayoutConstraint?
private var caretTrailingConstraint: 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() {
super.reset()
@ -135,16 +105,37 @@ open class TextLinkCaret: Control {
// MARK: - Overrides
//--------------------------------------------------
open override func updateView() {
label.surface = surface
label.disabled = disabled
label.text = text
var updatedText = text ?? ""
caretView.surface = surface
caretView.disabled = disabled
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 {
@ -221,6 +212,12 @@ internal class CaretView: View {
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
//------------------------------------------------------

View File

@ -12,11 +12,13 @@ public struct ImageLabelAttribute: AttachmentLabelAttributeModel {
public enum Error: Swift.Error {
case bundleNotFound
case imageNotFound(String)
case imageNotSet
}
public var id = UUID()
public var location: Int
public var length: Int
public var imageName: String
public var imageName: String?
public var image: UIImage?
public var frame: CGRect
public var tintColor: UIColor
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
}
public func getAttachment() throws -> 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)
}
private func imageAttachment(image: UIImage) -> NSTextAttachment {
let attachment = NSTextAttachment()
attachment.image = image.withTintColor(tintColor)
attachment.bounds = frame
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
}
}
}