Skip to content

Instantly share code, notes, and snippets.

View GE-N's full-sized avatar

Jerapong Nampetch GE-N

View GitHub Profile
@GE-N
GE-N / uitextfield-placeholder-color.swift
Last active May 20, 2016 13:57
Change UITextField's placeholder text colour
extension UITextField {
public override func drawPlaceholderInRect(rect: CGRect) {
let newColor = UIColor(white: 1, alpha: 0.4)
let range = NSMakeRange(0, self.attributedPlaceholder!.length)
var mutatedAttributedPlaceholder = NSMutableAttributedString(attributedString: self.attributedPlaceholder!)
mutatedAttributedPlaceholder.setAttributes([ NSForegroundColorAttributeName : newColor ], range: range)
self.attributedPlaceholder = mutatedAttributedPlaceholder
super.drawPlaceholderInRect(rect)
@GE-N
GE-N / uicolor-random.swift
Last active August 29, 2015 14:24
Random colour on Swift
let randomBgColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1)
@GE-N
GE-N / uitextfield-hidecursor.swift
Created July 12, 2015 01:38
Hidden UITextfield's blink cursor.
// Disable blink caret inspired from Joseph Chiu's answer on
// http://stackoverflow.com/questions/3699727/hide-the-cursor-of-an-uitextfield
extension UITextField {
public override func caretRectForPosition(position: UITextPosition!) -> CGRect {
if <# condition for hidden blink #> {
return CGRectZero
}
return super.caretRectForPosition(position)
}
@GE-N
GE-N / uicolor-hexcode.swift
Last active August 29, 2015 14:24
UIColor from Hex Code in Swift
// Inspiration from
// http://stackoverflow.com/questions/28644311/howto-get-the-rgb-code-int-from-an-uicolor-in-swift
// http://stackoverflow.com/questions/24074257/how-to-use-uicolorfromrgb-value-in-swift
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0))
@GE-N
GE-N / UIButton+StateBGColorV2.swift
Last active August 29, 2015 14:25
Change UIButton's background colour when highlighted
//
// UIButton+StateBGColor.swift
// ButtonExtension
//
// Created by Jerapong Nampetch on 7/19/2558 BE.
// Copyright (c) 2558 Jerapong Nampetch. All rights reserved.
//
import UIKit
@GE-N
GE-N / enum-http-request.swift
Last active August 29, 2015 14:25
Sample HTTPRequest with Swift's Enum as parameter
/**
Host: Staging => http://staging.myblog.com/api, Production => https://myblog.com/api
Method: /blog
Read blog — GET /blog/:id
Create blog — POST /blog, body: title=<string>&body=<string>
Update blog — UPDATE /blog/:id, body: title=<string>&body=<string>
Delete blog — DELETE /blog:id
*/
enum BlogMethod {
@GE-N
GE-N / enum-set-host.swift
Created July 26, 2015 08:01
Configure hosting by Swift's enum
enum Host {
case Staging = “http://staging.myblog.com/api”
case Production = “https://myblog.com/api”
}
class API {
class var host: Host
func blog(…) -> (…) {
let apiPath = “\(API.host.rawValue)/blog”
...
@GE-N
GE-N / blog-swift-enum-apicall.swift
Created July 26, 2015 08:10
Blog snipper call api by swift enum params
let api = API()
let blogID = "1"
let title = "Hello my blog"
let body = "Hello world, this is my first blog entry"
// Create blog
api.blog(.CREATE(title, body))
// Read blog
// (1)
// Get report by month
// without Enum
-(Report *)reportFor:(int)month {
...
}
Report *reportMay = self.reportFor(5) // Report for May
@GE-N
GE-N / Podfile-BDD
Last active August 29, 2015 14:26
Podfile for install quick & nimble
platform :ios, '8.0'
use_frameworks!
target 'SMSME' do
end
target 'SMSMETests' do
pod 'Quick', '~> 0.3.1' ## For Swift 1.2
pod 'Nimble'
end