Skip to content

Instantly share code, notes, and snippets.

func supportsKeyboard(lang: String?) -> Bool {
guard let _lang = lang else {
return false
}
let predicates = [
Predicate(format: "SELF == %@", "en"),
Predicate(format: "SELF like[cd] %@", "en-*")
]
for predicate in predicates {
if NSArray(arrayLiteral: _lang).filtered(using: predicate).count > 0 {
@wh1pch81n
wh1pch81n / UISearchBar_Magnifying_GlassColorChange.swift
Last active August 15, 2016 02:27
Ever want to change the color of the magnifying glass in the UISearchBar? Well now you can and you can change its color in interface builder!
extension UISearchBar {
/** An easy way to set the magnifying glass color in interface builder
It is recommended to set this in interface Builder,
However if you want to do it programatically you can do it like this:
```
let searchBar = UISearchBar()
searchBar.magnifyingGlassColor = UIColor.red
```
*/
@IBInspectable var magnifyingGlassColor: UIColor {
@wh1pch81n
wh1pch81n / UISearchBar_Magnifying_GlassColorChange_withSceneDock.swift
Created August 13, 2016 17:21
An alternative implementation for setting the magnifying glass color. You need to add this class to the scene dock of your View Controller in interface builder and connect the IBOutlet to the UISearchBar instance. The color for the component can be set on the object in storyboard. The although this is more work than the other approach you have t…
class UISearchBarMagnifyingGlass: NSObject {
@IBOutlet weak var searchBar: UISearchBar! {
didSet {
if let _color = color, let _searchBar = searchBar {
for i in _searchBar.subviews.first!.subviews {
if let textField = i as? UITextField,
let imageView = textField.leftView as? UIImageView,
let image = imageView.image
{
let coloredImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
@wh1pch81n
wh1pch81n / UIButtonCalbackExtension.swift
Last active September 3, 2016 04:18
UIButton uses an old objective-c style method to add new callbacks which requires selectors. If you would rather use a block based approach then consider the following approach. You can specify a swift closure to be executed when the button receives a touch event.
///////////////
// code example
//-------------
button.callback { _ in
print("hello world") // print "hello world" when touchUpInside event is recieved
}
button.callback(for: [.TouchDown]) { _ in
print("hello world") // print "hello world" when touchDownInside event is recieved
}
@wh1pch81n
wh1pch81n / InitializerProtocol.swift
Last active September 3, 2016 16:43
Initialize an NSObject and set up its properties with a block. Useful for class variables.
import UIKit
protocol Initializer {}
extension Initializer {
func with(bootStrap: (inout Self) -> ()) -> Self {
var s = self
bootStrap(&s)
return s
}
}
#if DEBUG
print("print debug value", x)
#endif
ABC_OBJC=ABC=1
ABC_SWIFT=ABC
#if ABC
// Code between here that runs only when ABC exists
#endif
// This is a conditional inclusion.
// It searches for a file called Captain.xcconfig that lives one file level up in relation to this file.
#include? "../Captain.xcconfig"
@wh1pch81n
wh1pch81n / page_names.swift
Last active October 8, 2016 03:12
An example of stringifying an enum string state
enum PageNames: String {
case sc🖍search = "MySearch"
var page_name: String {
return String(describing: self).replacingOccurrences(of: "🖍", with: "/")
}
}
PageNames.sc🖍search.rawValue // "MySearch"