Skip to content

Instantly share code, notes, and snippets.

@ElegyD
ElegyD / CountWords.kt
Created July 6, 2018 10:19
Counts the number of words in a String
import java.text.BreakIterator
fun String.countWords(): Int {
fun isWord(word: String): Boolean {
if (word.length == 1) {
return word[0].isLetterOrDigit()
}
return "" != word.trim()
}
@ElegyD
ElegyD / UnicodeFlag.kt
Created February 1, 2018 15:14
Get an Unicode flag for a locale identifier in Kotlin
/// Offsets every character in iso String by a fixed amount to get the regional indicator symbol.
/// iso needs to be uppercase. 'A' for example is then 🇦.
/// For example an iso "DE" returns "🇩🇪"
fun getUnicodeFlag(iso: String): String {
var flag = ""
val offset = 0x1F1A5
(0 until iso.length)
.map { Character.codePointAt(iso, it) + offset }
.map { Character.toChars(it) }
.forEach { flag += String(it) }
@ElegyD
ElegyD / UIImage+Resize.swift
Last active February 28, 2019 10:33
Extension to resize an UIImage to a maximum width/height while keeping the aspect ratio in Swift 4.
extension UIImage {
func resized(to maxSize: CGFloat) -> UIImage? {
var size = self.size
if size.width <= maxSize && size.height <= maxSize {
return self
}
let scaleFactor = maxSize / max(size.width, size.height)
size.width *= scaleFactor
size.height *= scaleFactor
@ElegyD
ElegyD / Sequence+GroupByDate.swift
Last active July 18, 2022 09:05
Group a Swift Model by day with ascending or descending order (Swift 5) E.g.: for a UITableView with sections
import UIKit
extension Sequence {
func groupByDay(dateKey: (Iterator.Element) -> Date, order: ComparisonResult = .orderedAscending) -> [[Iterator.Element]] {
var groups: [[Iterator.Element]] = []
for element in self {
let key = dateKey(element)
guard let dayIndex = groups.firstIndex(where: { $0.contains(where: { Calendar.current.isDate(dateKey($0), inSameDayAs: key) }) }) else {
guard let nextIndex = groups.firstIndex(where: { $0.contains(where: { dateKey($0).compare(key) == order }) }) else {
groups.append([element])
@ElegyD
ElegyD / String+Substring.swift
Created June 2, 2017 09:01
Swift 3 String substring extension for easier handling if offset exceeds endIndex
extension String {
/// Returns a new string containing the characters of the String from the one at a given index to the end. If 'from' exceeds endIndex it returns an empty string.
func substring(from: Int) -> String {
guard let offsetStartIndex = index(startIndex, offsetBy: from, limitedBy: endIndex) else { return "" }
return self[offsetStartIndex..<endIndex]
}
/// Returns a new string containing the characters of the String up to the one at a given index. If 'to' exceeds endIndex it returns the string as it was.
func substring(to: Int) -> String {
return self[startIndex..<(index(startIndex, offsetBy: to, limitedBy: endIndex) ?? endIndex)]
@ElegyD
ElegyD / LocationVCard.swift
Last active March 18, 2019 20:06 — forked from naturaln0va/LocationVCard.swift
Create a location vCard for use in UIActivityViewController like in Apple's Maps app.
import CoreLocation
static func vCardURL(from coordinate: CLLocationCoordinate2D, with name: String?) -> URL {
let vCardFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("vCard.loc.vcf")
let vCardString = [
"BEGIN:VCARD",
"VERSION:4.0",
"FN:\(name ?? "Shared Location")",
"item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)",
@ElegyD
ElegyD / SeparatorView.swift
Last active September 12, 2019 23:15 — forked from JULI-ya/SeparatorView.swift
SeparatorView in Swift 4 with drag indicator
import UIKit
enum SeparatorType {
case horizontal
case vertical
}
let kTotalSize: CGFloat = 18 // the total height of the separator (including parts that are not visible)
let kVisibleSize: CGFloat = 8 // the height of the visible portion of the separator
let kMargin: CGFloat = (kTotalSize - kVisibleSize) / 2 // the height of the non-visible portions of the separator (i.e. above and below the visible portion)