Skip to content

Instantly share code, notes, and snippets.

@frankchang0125
frankchang0125 / enum_count.swift
Created July 19, 2016 07:55
Trick to get the count of enum
enum Gender: Int {
case Male
case Female
/* Trick to get the count of enum */
static let count: Int = {
var max: Int = 0
while let _ = Gender(rawValue: max) { max += 1 }
return max
}()
@frankchang0125
frankchang0125 / switch_dict.swift
Last active July 20, 2016 07:39
Trick to switch case base on dictionary's key
enum DetailInfoRowType: Int {
case AddressRow
case PhoneRow
case OpeningHoursRow
case DiscountHoursRow
case PriceRangesRow
case IntroRow
}
let detailInfoRow = detailInfoRows[indexPath.row]
@frankchang0125
frankchang0125 / collectionView_setContentOffset.swift
Last active November 9, 2022 19:44
Scroll to the offset after reloading collectionView
/* We call layoutIfNeeded() immediately after reloadData()
* to force collection view to perform layout immediately
* This is required to let the setContentOffset() in dispatch block to be executed
* after collection view has completed performing its layout to scroll to the correct offset
* For further explanations, please see: http://goo.gl/BpzlA5 and http://goo.gl/6CH64b
*/
self.collectionView.reloadData()
self.collectionView.layoutIfNeeded()
dispatch_async(dispatch_get_main_queue(), {
@frankchang0125
frankchang0125 / tableView_setContentOffset.swift
Created July 28, 2016 17:10
Scroll to the offset after reloading tableView
/* We call layoutIfNeeded() immediately after reloadData()
* to force table view to perform layout immediately
* This is required to let the setContentOffset() in dispatch block to be executed
* after table view has completed performing its layout to scroll to the correct offset
* For further explanations, please see: http://goo.gl/BpzlA5 and http://goo.gl/6CH64b
*/
self.tableView.reloadData()
self.tableView.layoutIfNeeded()
dispatch_async(dispatch_get_main_queue(), {
@frankchang0125
frankchang0125 / textViewPlaceholder.swift
Last active August 14, 2016 09:45
Simulate placeholder effect for UITextView
func textViewDidBeginEditing(textView: UITextView) {
/* Simulate the placeholder effect */
if textView.text == <Simulated placeholder> {
textView.text = ""
textView.textColor = UIColor.blackColor()
}
}
func textViewDidEndEditing(textView: UITextView) {
/* Simulate the placeholder effect */
@frankchang0125
frankchang0125 / uitextfield_didChange.swift
Created August 19, 2016 06:56
Detect UITextField change event
textField.addTarget(self, action: #selector(textFieldEditingChanged(_:)),
forControlEvents: .EditingChanged)
@frankchang0125
frankchang0125 / dismiss_keyboard_without_first_responder.swift
Last active August 19, 2016 07:30
Trick to dismiss keyboard without knowing who's first responder
/* Dismiss keyboard */
UIApplication.sharedApplication().sendAction(#selector(resignFirstResponder), to: nil, from: nil, forEvent: nil)
@frankchang0125
frankchang0125 / remove_plain_tableView_empty_cells.swift
Created September 17, 2016 02:15
Trick to remove the plain tableView empty cells
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.tableFooterView = UIView()
}
@frankchang0125
frankchang0125 / heightOfText.swift
Last active October 12, 2016 04:30
Calculate text height with given width and font
/* Get the height of ONE line text with given width and font */
func heightOfOneLineString(text: String, width: CGFloat, font: UIFont) -> CGFloat {
let rectOneLine = NSString(string: text).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)),
options: .truncatesLastVisibleLine,
attributes: [ NSFontAttributeName: font ],
context: nil)
return rectOneLine.size.height
}
/* Get the height of MULTIPLE lines text with given width and font */
@frankchang0125
frankchang0125 / textfiled_integer_only.swift
Created November 7, 2016 14:50
Restrict text field input to be integer value only
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
/* Allow integer only */
let invalidCharacters = CharacterSet.decimalDigits.inverted
return string.rangeOfCharacter(from: invalidCharacters) == nil
}