Skip to content

Instantly share code, notes, and snippets.

View hsleedevelop's full-sized avatar
🔥

HS Lee hsleedevelop

🔥
View GitHub Profile
extension XCUIElement {
func clearText(andReplaceWith newText:String? = nil) {
tap()
press(forDuration: 1.0)
var select = XCUIApplication().menuItems["Select All"]
if !select.exists {
select = XCUIApplication().menuItems["Select"]
}
//For empty fields there will be no "Select All", so we need to check
@hsleedevelop
hsleedevelop / wkWebView.m
Last active July 21, 2020 07:21
WKWebView not opening links with target=“_blank”?
//My solution is to cancel the navigation and load the request with loadRequest: again. This will be come the similar //behavior like UIWebView which always open new window in the current frame.
//
//Implement the WKUIDelegate delegate and set it to _webview.uiDelegate. Then implement:
//https://stackoverflow.com/a/25853806/3374327
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
class UserInfoValidator {
enum Validation {
case nickname
var pattern: String {
switch self {
case .nickname:
return "^[a-zA-Z0-9ㄱ-ㅣ가-힣\\-\\.\\_]{1,16}$"
default:
return ""
extension UILabel {
@objc
func set(underLineFor keyword: String) {
guard let text = self.text ?? self.attributedText?.string else { return }
let attributedString = self.attributedText.map(NSMutableAttributedString.init(attributedString:)) ?? NSMutableAttributedString(string: text)
let indices = attributedString.string.indicesOf(string: keyword)
let length = keyword.count
@hsleedevelop
hsleedevelop / UIViewController+Extension.swift
Created April 23, 2020 01:23
ViewController Extension
extension UIViewController {
func isVisible() -> Bool {
if #available(iOS 9.0, *) {
if let view = self.viewIfLoaded {
return view.window != nil
}
return false
} else {
@hsleedevelop
hsleedevelop / ViewController.swift
Last active February 13, 2020 11:04
embed a viewController to another viewController
extension UIViewController {
func embed(_ vc: UIViewController, in _containerView: UIView? = nil) {
let containerView: UIView = _containerView ?? view // Use the whole view if no container view is provided.
containerView.addSubview(vc.view)
NSLayoutConstraint.activate([
vc.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0),
vc.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0),
vc.view.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0),
vc.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0)
])
extension UIView {
func superview<T>(of type: T.Type) -> T? {
return superview as? T ?? superview.flatMap { $0.superview(of: type) }
}
}
// Example usage:
func textFieldDidBeginEditing(_ textField: UITextField) {
// Get the cell containing the textfield.
if let cell = textField.superview(of: TextFieldTableViewCell.self) {
@hsleedevelop
hsleedevelop / BetterXcodeJumpToCounterpartSwift.org
Created November 20, 2019 06:26 — forked from danielmartin/BetterXcodeJumpToCounterpartSwift.org
Add support for a better Xcode's Jump to Next Counterpart in Swift

If you work on a Swift project that follows the Model-View-ViewModel (MVVM) architecture or similar, you may want to jump to counterpart in Xcode from your view to your model, and then to your view model. (ie. by using Ctrl+Cmd+Up and Ctrl+Cmd+Down).

You can do this in recent versions of Xcode by setting a configuration default.

From a terminal, just type this command and press Enter:

defaults write com.apple.dt.Xcode IDEAdditionalCounterpartSuffixes -array-add "ViewModel" "View"
typealias CustomLabelTouchHandler = (CustomLabel) -> Void
typealias CustomLabelTouchIndexHandler = (CustomLabel, Int) -> Void
private var touchHandler: CustomLabelTouchHandler?
private var touchHandlerWithIndex: CustomLabelTouchIndexHandler?
///Never ever use ide attribute text, then will trigger correctly
@objc func didLabelTouchedWidthIndexHandler(_ gestureRecognizer: UIGestureRecognizer) {
guard let attributedString = self.attributedText, let font = self.font else { return }
@hsleedevelop
hsleedevelop / gist:ccaebcb339b8d6ff7904ebe59dba80c0
Created October 11, 2019 03:09 — forked from steipete/ios-xcode-device-support.sh
Using iOS 13.2 devices with Xcode 11.1 (instead of Xcode 11.2) (also, Xcode 10.3)
// The trick is to link the DeviceSupport folder from the beta to the stable version.
// sudo needed if you run the Mac App Store version. Always download the dmg instead... you'll thank me later :)
// Support iOS 13.2 devices (Xcode 11.2) with Xcode 11.1:
sudo ln -s /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/13.2 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
// Xcode 10.3 to Xcode 11
sudo ln -s /Applications/Xcode-11.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/13.0 /Applications/Xcode-10.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
// Then restart Xcode and reconnect your devices. You will need to do that for every beta of future iOS versions