Created
November 7, 2022 21:07
-
-
Save yccheok/ad35f6f9fbb47631f1bc57184845fb46 to your computer and use it in GitHub Desktop.
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
// | |
// ShareViewController.swift | |
// share | |
// | |
// Created by Yan Cheng Cheok on 03/11/2022. | |
// | |
import UIKit | |
import lib | |
import CoreServices | |
class ShareViewController: UIViewController { | |
private let typeURL = String(kUTTypeURL) | |
@IBOutlet weak var titleTextView: UITextView! | |
@IBOutlet weak var bodyTextView: LinedTextView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initTitleTextView() | |
initBodyTextView() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
// Get the all encompasing object that holds whatever was shared. If not, dismiss view. | |
guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem, | |
let itemProvider = extensionItem.attachments?.first else { | |
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) | |
return | |
} | |
// Check if object is of type text | |
if itemProvider.hasItemConformingToTypeIdentifier(typeURL) { | |
handleIncomingURL(itemProvider: itemProvider) | |
} else { | |
print("Error: No url or text found") | |
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) | |
} | |
} | |
private func handleIncomingURL(itemProvider: NSItemProvider) { | |
print(">>>> handleIncomingURL") | |
itemProvider.loadItem(forTypeIdentifier: typeURL, options: nil) { (item, error) in | |
if let error = error { | |
print(">>>> URL-Error: \(error.localizedDescription)") | |
} | |
if let url = item as? NSURL, let urlString = url.absoluteString { | |
print(">>>> handleIncomingURL \(urlString)") | |
} | |
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) | |
} | |
} | |
@IBAction func closeButtonClicked(_ sender: Any) { | |
print(">>>> closeButtonClicked") | |
extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) | |
} | |
@IBAction func doneButtonClicked(_ sender: Any) { | |
print(">>>> doneButtonClicked") | |
extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) | |
} | |
private func initTitleTextView() { | |
titleTextView.font = ShareViewController.getTitleEditFont() | |
// Leading/ trailing spacing. | |
titleTextView.textContainer.lineFragmentPadding = 16 | |
titleTextView.delegate = self | |
} | |
private func initBodyTextView() { | |
bodyTextView.font = ShareViewController.getBodyEditFont() | |
bodyTextView.delegate = self | |
// Need not set bodyTextView.textContainer.lineFragmentPadding. It is called within LineTextView. | |
} | |
private static func getTitleEditFont() -> UIFont { | |
let sfFont = UIFont.systemFont(ofSize: 21, weight: .semibold) | |
let fontDescriptor = sfFont.fontDescriptor | |
if let newYorkFontDescriptor = fontDescriptor.withDesign(.serif) { | |
let newYorkFont = UIFont(descriptor: newYorkFontDescriptor, size: 0.0) | |
return newYorkFont | |
} else { | |
return sfFont | |
} | |
} | |
private static func getBodyEditFont() -> UIFont { | |
let sfFont = UIFont.systemFont(ofSize: 19) | |
let fontDescriptor = sfFont.fontDescriptor | |
if let newYorkFontDescriptor = fontDescriptor.withDesign(.serif) { | |
let newYorkFont = UIFont(descriptor: newYorkFontDescriptor, size: 0.0) | |
return newYorkFont | |
} else { | |
return sfFont | |
} | |
} | |
} | |
extension ShareViewController: UITextViewDelegate { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment