This file contains 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
| // | |
| // BLOGMainViewController.swift | |
| // BlogApp | |
| // | |
| // Created by Steven Troughton-Smith on 30/12/2021. | |
| // | |
| // | |
| import UIKit | |
| class CTDLTokenAttachmentView: UIView { | |
| let label = UILabel() | |
| let contentView = UIView() | |
| init() { | |
| super.init(frame: .zero) | |
| addSubview(contentView) | |
| contentView.addSubview(label) | |
| label.font = UIFont.systemFont(ofSize: UIFloat(14)) | |
| label.textColor = .white | |
| contentView.backgroundColor = .systemBlue | |
| contentView.layer.cornerRadius = UIFloat(8) | |
| contentView.layer.cornerCurve = .continuous | |
| contentView.layer.masksToBounds = true | |
| isUserInteractionEnabled = false | |
| } | |
| required init?(coder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| // MARK: - | |
| override func layoutSubviews() { | |
| let padding = UIFloat(4) | |
| var newFrame = bounds | |
| newFrame.origin.y = UIFloat(8) | |
| contentView.frame = newFrame | |
| label.frame = contentView.bounds.insetBy(dx: padding, dy: padding) | |
| } | |
| var suggestedBounds: CGRect { | |
| let padding = UIFloat(4) | |
| var f = label.frame | |
| f.size.width += padding * 2 | |
| f.size.height += padding * 2 | |
| return f | |
| } | |
| } | |
| class CTDLTokenAttachmentProvider: NSTextAttachmentViewProvider { | |
| override init(textAttachment: NSTextAttachment, parentView: UIView?, textLayoutManager: NSTextLayoutManager?, location: NSTextLocation) { | |
| super.init(textAttachment: textAttachment, parentView: parentView, textLayoutManager: textLayoutManager, location: location) | |
| let tv = CTDLTokenAttachmentView() | |
| if let data = textAttachment.contents { | |
| tv.label.text = String(data: data, encoding: .utf8) | |
| } | |
| tv.label.sizeToFit() | |
| textAttachment.bounds = tv.suggestedBounds | |
| view = tv | |
| } | |
| } | |
| final class CTDLRecipeEditorViewController: UIViewController, UITextViewDelegate, UIPopoverPresentationControllerDelegate { | |
| let textView = UITextView() | |
| let sampleText = """ | |
| """ | |
| init() { | |
| super.init(nibName: nil, bundle: nil) | |
| textView.delegate = self | |
| textView.font = UIFont.systemFont(ofSize: UIFloat(16)) | |
| NSTextAttachment.registerViewProviderClass(CTDLTokenAttachmentProvider.self, forFileType: "public.text") | |
| let attributedString = NSMutableAttributedString(string:sampleText) | |
| attributedString.setAttributes([.foregroundColor:UIColor.label, .font:textView.font!], range: NSRange(location: 0, length: attributedString.length)) | |
| textView.attributedText = attributedString | |
| view.addSubview(textView) | |
| } | |
| // MARK: - | |
| override func viewDidLayoutSubviews() { | |
| textView.frame = view.bounds | |
| textView.contentInsetAdjustmentBehavior = .always | |
| } | |
| func insertToken(string:String) { | |
| let attachment = NSTextAttachment() | |
| attachment.fileType = "public.text" | |
| attachment.contents = string.data(using: .utf8) | |
| let attachmentString = NSAttributedString(attachment: attachment) | |
| let mutableContents = NSMutableAttributedString(attributedString: self.textView.attributedText) | |
| var selectedRange = textView.selectedRange | |
| if selectedRange.location + 1 <= textView.attributedText.string.count { | |
| selectedRange.length = 1 | |
| mutableContents.insert(attachmentString, at: selectedRange.location) | |
| } | |
| else { | |
| mutableContents.append(attachmentString) | |
| } | |
| textView.attributedText = mutableContents | |
| selectedRange.length = 0 | |
| selectedRange.location += 1 | |
| textView.selectedRange = selectedRange | |
| textView.layoutManager.invalidateLayout(forCharacterRange: NSRange(location: 0, length: textView.attributedText.length), actualCharacterRange: nil) | |
| textView.becomeFirstResponder() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment