Skip to content

Instantly share code, notes, and snippets.

<?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"
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)
@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)
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
}
}
// 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
// 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")
<?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
<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>
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"
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)
}