Skip to content

Instantly share code, notes, and snippets.

import XCTest
let searchTerm = "Lorem"
let targetString = "Lorem Ipsum is ... 😀"
let expectedResult = "L̲o̲r̲e̲m̲ Ipsum is ... 😀"
let attributedString = NSMutableAttributedString.generateAttributedString(with: searchTerm, targetString: targetString)
// Convert attributed string to Unicode text.
let mockUnderlineUnicodeText = generateUnderlineUnicodeText(from: attributedString!, by: NSAttributedString.Key.font)
func generateUnderlineUnicodeText(from attributedString: NSAttributedString, by key: NSAttributedString.Key) -> String {
var keyRanges = [NSRange]()
// Find all attributes by difference (key) and take ranges
attributedString.enumerateAttributes(in: NSRange(location: 0, length: attributedString.length)) { (attributes, range, _) in
attributes.forEach { (key, value) in
switch key {
case key: keyRanges.append(range)
default: print("Unknown attribute found in the attributed string")}
extension NSMutableAttributedString {
static func generateAttributedString(with searchTerm: String, targetString: String) -> NSMutableAttributedString? {
let attributedString = NSMutableAttributedString(string: targetString)
do {
// Your more complicated expressions here
let regex = try NSRegularExpression(pattern: searchTerm, options: .caseInsensitive)
extension String {
init<S: Sequence>(unicodeScalars ucs: S) where S.Iterator.Element == UnicodeScalar {
var s = ""
s.unicodeScalars.append(contentsOf: ucs)
self = s
}
var underlineUnicode: String? {
let underlineUnicode: UInt32 = 818
@eastari
eastari / It_took_ms.swift
Created November 20, 2017 09:25
It_took_ms
let startTime = CFAbsoluteTimeGetCurrent()
// to do smth
let endTime = CFAbsoluteTimeGetCurrent()
let elapsedTime = (endTime - startTime) * 1000
print("It took \(elapsedTime) ms")
@eastari
eastari / SourceEditorCommand.swift
Created October 6, 2017 12:50
Part Xcamelize 2
class SourceEditorCommand: NSObject, XCSourceEditorCommand {
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Swift.Void ) {
...
}
func handle(range: XCSourceTextRange, inBuffer buffer: XCSourceTextBuffer) -> () {
...
}
@eastari
eastari / Camelize.swift
Last active October 6, 2017 12:26
Part of XCamelize
public extension String {
func camelize() -> String {
let source = removeFirstSpaces(clean(with: " ", allOf: "-", "_", "."))
if source.characters.contains(" ") {
let first = source[self.startIndex...self.index(after: startIndex)].lowercased()
let cammel = source.capitalized.replacingOccurrences(of: " ", with: "")
let rest = String(cammel.characters.dropFirst())
...
let isPhone = UIDevice.current.userInterfaceIdiom == .phone ? true : false
...
class RootRouter {
var panelScreen : PanelModuleInput?
func setupRouter(window: UIWindow) {
panelScreen = PanelAssembly.createModule()
class PanelAssembly {
class func createModule() -> PanelModuleInput {
let presenter = PanelPresenter()
let interactorMidi = PanelInteractorMidi()
let interactorInstrument = PanelInteractorInstrument()
let router = PanelRouter()
let soundManager = SoundManager()