Skip to content

Instantly share code, notes, and snippets.

View activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
View label_tap_location.swift
extension UILabel {
func characterIndexTapped(tap: UITapGestureRecognizer) -> Int? {
guard attributedText != nil else { return nil }
// Create instances for NSLayoutManager, NSTextContainer, and NSTextStorage
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: .zero)
let textStorage = NSTextStorage(attributedString: attributedText!)
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
View html_label_tap.swift
@objc func didTapLabel(sender: UITapGestureRecognizer) {
var tappedImage = false
if let index = label.characterIndexTapped(tap: sender), let _ = label.attributedText?.attribute(NSAttributedString.Key.attachment, at: index, effectiveRange: nil) as? NSTextAttachment {
tappedImage = true
}
let alertTitle = tappedImage ? "Tapped Image":"Random Taps"
let alertMessage = tappedImage ? "You just tapped the image.":"You just tapped outside the image."
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
View html_string_conversion.swift
extension String {
func convertToAttributedFromHTML() -> NSAttributedString? {
var attributedText: NSAttributedString?
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue]
if let data = data(using: .unicode, allowLossyConversion: true), let attrStr = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
attributedText = attrStr
}
return attributedText
}
}
View html_label_setup.swift
// label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
// label.text = "Hello World!"
// label.textColor = .black
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
label.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
label.isUserInteractionEnabled = true
View hashable_dictionary_example.swift
// instantiate a dictionary
var personalInfo = ["firstName": "John", "address": "1100 Main St."]
// data retrieval
let firstName = personalInfo["firstName"]
let middleName = personalInfo["middleName"]
// data insertion
personalInfo["lastName"] = "Smith"
personalInfo["firstName"] = "Mike"
// data deletion
let deletedAddress = personalInfo.removeValue(forKey: "address")
View android_biometric.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
View quick_actions.xml
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemType</key>
<string>SearchAction</string>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeSearch</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Search</string>
<key>UIApplicationShortcutItemSubtitle</key>
View quick_actions.swift
func sceneWillResignActive(_ scene: UIScene) {
var shortcutItems = UIApplication.shared.shortcutItems ?? []
if shortcutItems.isEmpty {
shortcutItems += [
UIApplicationShortcutItem(type: "Test Type 0", localizedTitle: "Test Title 0"),
UIApplicationShortcutItem(type: "Test Type 1", localizedTitle: "Test Title 1")
]
} else {
if let mutableShortcutItem = shortcutItems.first?.mutableCopy() as? UIMutableApplicationShortcutItem {
mutableShortcutItem.type = "Updated Type 0"
View quick_action_tapped.swift
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: "Alert", message: "performActionFor \(shortcutItem.type)", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
window?.rootViewController?.present(alertController, animated: true, completion: nil)
}