Using Stride in Swift 2.0

It changed a bit, here is the new syntax: 0.stride(to: 10, by: 2) and Array(0.stride(to: 10, by: 2)) // is [0, 2, 4, 6, 8] if you take a look at here, you can see what types conform to the Strideable protocol. As @RichFox pointed out, in Swift 3.0 the syntax changed back to the … Read more

How to test that staticTexts contains a string using XCTest

You can use NSPredicate to filter elements. let searchText = “the content of the staticText” let predicate = NSPredicate(format: “label CONTAINS[c] %@”, searchText) let elementQuery = app.staticTexts.containing(predicate) if elementQuery.count > 0 { // the element exists } With CONTAINS[c] you specify that the search is case insensitive. Have a look at Apples Predicate Programming Guide

NSString boundingRectWithSize:options:attributes:context: not usable in Swift?

The initializers expect named arguments. extension UIFont { func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize { return NSString(string: string).boundingRectWithSize(CGSize(width: width, height: DBL_MAX), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: self], context: nil).size } } Note: Strings can be cast to NSStrings. extension UIFont { func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize { return (string … Read more

How do you detect a SwiftUI touchDown event with no movement or duration?

You can use the .updating modifier like this: struct TapTestView: View { @GestureState private var isTapped = false var body: some View { let tap = DragGesture(minimumDistance: 0) .updating($isTapped) { (_, isTapped, _) in isTapped = true } return Text(“Tap me!”) .foregroundColor(isTapped ? .red: .black) .gesture(tap) } } Some notes: The zero minimum distance makes … Read more

Rounded Borders in SwiftUI

That’s not a workaround, it’s how you do it in SwiftUI. Two things: There used to be a cornerRadius modifier that became deprecated in… beta 4? beta 5? Yes, it’s been a moving target. With a great amount of thanks to @kontiki (blog post), here’s an extension that nicely returns what you want: extension View … Read more

How to detect if keyboard is present in swiftui

Using this protocol, KeyboardReadable, you can conform to any View and get keyboard updates from it. KeyboardReadable protocol: import Combine import UIKit /// Publisher to read keyboard changes. protocol KeyboardReadable { var keyboardPublisher: AnyPublisher<Bool, Never> { get } } extension KeyboardReadable { var keyboardPublisher: AnyPublisher<Bool, Never> { Publishers.Merge( NotificationCenter.default .publisher(for: UIResponder.keyboardWillShowNotification) .map { _ in … Read more