Skip to content

Instantly share code, notes, and snippets.

@sanghapark
sanghapark / .swift
Created March 31, 2017 09:42
[iOS] figure out UITextView's height based on a given string with value
func calculateTextViewHeight(inset: UIEdgeInsets, lineFragmentPadding: CGFloat = 5.0, maxWidth: CGFloat, text: String, font: UIFont) -> CGSize {
let widthWithoutInset = maxWidth - (inset.left + inset.right + (2 * lineFragmentPadding))
let contraintRect = CGSize(width: widthWithoutInset, height: CGFloat.max)
let style = NSMutableParagraphStyle()
style.lineBreakMode = .ByWordWrapping
let attributes = [NSFontAttributeName: font, NSParagraphStyleAttributeName: style]
let boudingBox = NSString(string: text).boundingRectWithSize(contraintRect, options: .UsesLineFragmentOrigin, attributes: attributes, context: nil)
let newWidth = (boudingBox.size.width + (inset.left + inset.right + (2 * lineFragmentPadding) + 0.25)).roundNearest(0.5)
let newHeight = (boudingBox.size.height + ((inset.top + inset.bottom) + 0.25)).roundNearest(0.5)
return CGSize(width: newWidth, height: newHeight)
@sanghapark
sanghapark / .swift
Created March 31, 2017 01:50
[iOS] figure out UILabel height based on a given string
extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.height
}
}
@sanghapark
sanghapark / CapturingViewController.swift
Last active March 2, 2017 03:38
[iOS] Capturing still Images with focus & exposure
import UIKit
import AVFoundation
import Photos
class ViewController: UIViewController {
var focusMarker: UIImageView!
var exposureMarker: UIImageView!
var resetMarker: UIImageView!
private var adjustingExposureContext: String = ""
@sanghapark
sanghapark / AsyncOperation.swift
Created February 20, 2017 09:31
AsyncOperation subclassing Operation for asynchronous operation
class AsyncOperation: Operation {
enum State: String {
case Ready, Executing, Finished
fileprivate var keyPath: String {
return "is" + rawValue
}
}
var state = State.Ready {
willSet {
@sanghapark
sanghapark / .bash_profile
Created February 20, 2017 02:35
Git Branch Name to Terminal Prompt with Coloring
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\e[91m\]\$(parse_git_branch)\[\033[00m\] $ "
@sanghapark
sanghapark / asyncSyncFunction.swift
Created February 18, 2017 18:15
Make sync function async
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let userQueue = DispatchQueue.global(qos: .userInitiated)
let defaultQueue = DispatchQueue.global()
import Foundation
import Alamofire
enum NetworkStatus {
case WWAN
case EthernetOrWiFi
case NotReachable
case Unknown
}
@sanghapark
sanghapark / FibonacciGenerator.swift
Created August 10, 2016 19:07
generate fibonacci numbers using Swift
protocol GeneratorType {
associatedtype Element
mutating func next() -> Element?
}
class FibsGenerator: GeneratorType {
var state = (0, 1)
func next() -> Int? {
let upcomingNumber = state.0
state = (state.1, state.0 + state.1)
@sanghapark
sanghapark / videoOrientationAndPosition.swift
Last active August 10, 2016 09:04
get video orientaiton and position (back or front camera)
extension AVAsset {
func videoOrientation() -> (orientation: UIInterfaceOrientation, device: AVCaptureDevicePosition) {
var orientation: UIInterfaceOrientation = .Unknown
var device: AVCaptureDevicePosition = .Unspecified
let tracks :[AVAssetTrack] = self.tracksWithMediaType(AVMediaTypeVideo)
if let videoTrack = tracks.first {
let t = videoTrack.preferredTransform