Skip to content

Instantly share code, notes, and snippets.

View Jegge's full-sized avatar

Sebastian Boettcher Jegge

View GitHub Profile
@Jegge
Jegge / NSAttributedStringExtensions.swift
Created February 13, 2022 17:50
Some helpers to manipulate the attributes of a NSAttributedString
extension NSAttributedString {
convenience init? (html: String) {
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
guard
let data = html.data(using: .utf16, allowLossyConversion: true),
let text = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil)
else {
return nil
}
@Jegge
Jegge / BulletizeStringsExtension.swift
Created February 5, 2022 09:17
Creates an unordered list with bullets in a NSAttributesString from an array of strings
import UIKit
extension Collection where Element == String {
func bulletize(indentation: CGFloat, font: UIFont, color: UIColor) -> NSAttributedString {
let style = NSMutableParagraphStyle()
style.defaultTabInterval = indentation
style.firstLineHeadIndent = 0
style.lineSpacing = 0
style.paragraphSpacing = 15
style.headIndent = indentation
@Jegge
Jegge / CodableUserDefaults.swift
Created January 30, 2022 15:37
Allows to store / retrieve arbitrary Codable objects to / from UserDefaults.
extension UserDefaults {
func set<T>(encodable object: T, forKey key: String) where T: Encodable {
if let data = try? JSONEncoder().encode(object) {
set(data, forKey: key)
}
}
func decodable<T>(forKey key: String) -> T? where T: Decodable {
if let data = data(forKey: key) {
return try? JSONDecoder().decode(T.self, from: data)
@Jegge
Jegge / TextReader.swift
Created September 8, 2019 08:12
A TextReader reads single characters, full lines or complete texts from an URL or file with an arbitrary string encoding and line delimiter
//
// TextReader.swift
//
// Created by Sebastian Boettcher on 05.09.19.
// Copyright © 2019 Sebastian Boettcher. All rights reserved.
//
import Foundation
private func isReadable (fileHandle: FileHandle) -> Bool {
@Jegge
Jegge / DirectoryWatcher.swift
Created December 24, 2017 10:06
`DirectoryWatcher` can watch a directory and post a `DirectoryWatcher.didChangeNotification` notification when it's content changed.
//
// DirectoryWatcher.swift
//
// Created by Sebastian Boettcher on 16.07.17.
// Copyright © 2017 Sebastian Boettcher. All rights reserved.
//
// License: MIT
//
import Foundation
@Jegge
Jegge / Localizable.strings
Last active February 5, 2023 19:16
Localized enums in Swift
"Test_a" = "AAAAA";
"Test_b" = "BBBBB";
"Test_c" = "CCCCC";
"Test_d" = "DDDDD";
@Jegge
Jegge / keybase.md
Created November 6, 2017 12:48
keybase.md

Keybase proof

I hereby claim:

  • I am jegge on github.
  • I am jegge (https://keybase.io/jegge) on keybase.
  • I have a public key ASBlEFRs-Gw_0MYWU5Q_TAufkKsaIoIgMJKIcew84OTZRwo

To claim this, I am signing this object: