diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index 862fd829..81159965 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -25,6 +25,7 @@ public typealias ActionBlock = () -> () /// Set this to use a custom sizing object during updateView instead of the standard. public var sizeObject: MFSizeObject? public var scaleSize: NSNumber? + public var hero: Int? // Used for scaling the font in updateView. private var originalAttributedString: NSAttributedString? @@ -562,6 +563,17 @@ extension Label { clauses = [] Label.setUILabel(self, withJSON: json, delegate: delegateObject, additionalData: additionalData) originalAttributedString = attributedText + + hero = json?["hero"] as? Int + } + + func noticeBounds(_ rect: CGRect, color: UIColor) -> UIView { + + let view = UIView(frame: rect) + view.layer.borderColor = color.cgColor + view.layer.borderWidth = 1.0 + + return view } public func setAsMolecule() { diff --git a/MVMCoreUI/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Templates/MoleculeListTemplate.swift index 0270cb63..0db27a35 100644 --- a/MVMCoreUI/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Templates/MoleculeListTemplate.swift @@ -88,6 +88,38 @@ open class MoleculeListTemplate: ThreeLayerTableViewController { return cell } + open override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { + + cell.layoutIfNeeded() + let heroLabel = findHeroLabel(views: cell.contentView.subviews) + let rect = Label.boundingRect(forCharacterRange: NSRange(location: heroLabel!.hero!, length: 1), in: heroLabel!) + let rectView = heroLabel?.noticeBounds(rect, color: .clear) + heroLabel?.addSubview(rectView!) + let convert = cell.contentView.convert(rectView!.center, from: heroLabel) + + cell.accessoryView?.center.y = convert.y + rectView?.removeFromSuperview() + } + + // TODO: Write recursively. + func findHeroLabel(views: [UIView]) -> Label? { + + var queue: [UIView] = views + + repeat { + for view in queue { + if let label = view as? Label, label.hero != nil { + return label + } else if !view.subviews.isEmpty { + queue.append(contentsOf: view.subviews) + } + queue.removeFirst() + } + } while (!queue.isEmpty) + + return nil + } + open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) as? MoleculeListCellProtocol { cell.didSelectCell?(atIndex: indexPath, delegateObject: delegateObject() as? MVMCoreUIDelegateObject, additionalData: nil) @@ -111,11 +143,24 @@ open class MoleculeListTemplate: ThreeLayerTableViewController { if let tableView = tableView { let point = molecule.convert(molecule.bounds.origin, to: tableView) if let indexPath = tableView.indexPathForRow(at: point), tableView.indexPathsForVisibleRows?.contains(indexPath) ?? false { + //------------- tableView.beginUpdates() +// let cell = tableView.cellForRow(at: indexPath) +// cell.layoutIfNeeded() +// let heroLabel = findHeroLabel(views: cell.contentView.subviews) +// let rect = Label.boundingRect(forCharacterRange: NSRange(location: heroLabel!.hero!, length: 1), in: heroLabel!) +// let rectView = heroLabel?.noticeBounds(rect, color: .blue) +// heroLabel?.addSubview(rectView!) +// // let rect = Label.boundingRect(forCharacterRange: NSRange(location: 46, length: 6), in: self) +// let convert = cell.contentView.convert(rectView!.center, from: heroLabel) +// +// cell.accessoryView?.center.y = convert.y + /// ------------ tableView.endUpdates() } } } + open override func addMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { // This dispatch is needed to fix a race condition that can occur if this function is called during the table setup.