Skip to content

Instantly share code, notes, and snippets.

View usagimaru's full-sized avatar
🐰

usagimaru usagimaru

🐰
View GitHub Profile
@usagimaru
usagimaru / esc.json
Last active February 5, 2018 09:53
complex_modifications for Karabiner-Elements: [control] [return] to [esc]
{
"title": "カスタム",
"rules": [
{
"description": "control + return to esc",
"manipulators": [
{
"from": {
"key_code": "return_or_enter",
"modifiers": {
@usagimaru
usagimaru / paragraph_style.swift
Created March 3, 2018 20:24
CTParagraphStyle
var minimumLineHeight: CGFloat = 0
var maximumLineHeight: CGFloat = 0
let paragraphSettings: [CTParagraphStyleSetting] = [
CTParagraphStyleSetting(spec: .minimumLineHeight, valueSize: MemoryLayout<CGFloat>.size, value: &minimumLineHeight),
CTParagraphStyleSetting(spec: .maximumLineHeight, valueSize: MemoryLayout<CGFloat>.size, value: &maximumLineHeight)
]
// sizeof(T) ... MemoryLayout<T>.size
let paragraphStyle = CTParagraphStyleCreate(paragraphSettings, paragraphSettings.count)
@usagimaru
usagimaru / NSView+BackgroundColor.swift
Created March 10, 2018 17:20
IBInspectable backgroundColor for NSView
import Cocoa
extension NSView {
@IBInspectable var backgroundColor: NSColor? {
get {
guard let layer = layer, let backgroundColor = layer.backgroundColor else {return nil}
return NSColor(cgColor: backgroundColor)
}
set {
@usagimaru
usagimaru / ScrollsToTopResetter.swift
Last active March 10, 2018 17:25
Reset `scrollsToTop` flags to all scrollviews
import UIKit
extension UIView {
func resetScrollsToTop(_ targetScrollView: UIScrollView) {
if self is UIScrollView {
(self as! UIScrollView).scrollsToTop = self == targetScrollView
}
for v in subviews {
v.resetScrollsToTop(targetScrollView)
@usagimaru
usagimaru / UIImage+TintColor.swift
Created March 10, 2018 17:29
Paint UIImage with any UIColor
import UIKit
extension UIImage {
func paintedImage(color: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let bounds = CGRectMake(0, 0, size.width, size.height)
let context = UIGraphicsGetCurrentContext()
@usagimaru
usagimaru / NSView_optimize_drawRect.swift
Last active April 6, 2018 17:41
Optimize NSView's drawRect
class MyView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard let ctx = NSGraphicsContext.current?.cgContext else {return}
var rects: UnsafePointer<NSRect>?
var count = 0
self.getRectsBeingDrawn(&rects, count: &count)
@usagimaru
usagimaru / MyConstraintLayoutManager.swift
Created April 17, 2018 17:03
Disable implicit animations of CAConstraintLayoutManager.
import Cocoa
class MyConstraintLayoutManager: CAConstraintLayoutManager {
var isImplicitAnimationsDisabled: Bool = true
override func layoutSublayers(of layer: CALayer) {
// サブレイヤーのレイアウト時の暗黙アニメーションを無効化
CATransaction.begin()
CATransaction.setDisableActions(self.isImplicitAnimationsDisabled)
@usagimaru
usagimaru / custom.json
Created February 4, 2018 09:11
complex_modifications for Karabiner-Elements
{
"title": "カスタム",
"rules": [
{
"description": "左option: 英数/右command: かな",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "left_option",
@usagimaru
usagimaru / system_animation.swift
Last active August 9, 2018 09:03
Tips of a motion like iOS system transition.
// https://qiita.com/usagimaru/items/4306f261457e82641e4a
UIView.perform(.delete,
on: [],
options: [.beginFromCurrentState, .allowUserInteraction],
animations: {
aView.frame = targetFrame
},
completion: nil)
@usagimaru
usagimaru / NSBezierPath+SmoothRoundedRect.swift
Last active November 14, 2018 19:22
A Swift extension of NSBezierPath for drawing the smooth rounded rectangles like iOS 7 icons.
import Cocoa
extension NSBezierPath {
var cgPath: CGPath {
// https://stackoverflow.com/questions/1815568/how-can-i-convert-nsbezierpath-to-cgpath
let path = CGMutablePath()
var points = [CGPoint](repeating: .zero, count: 3)
for i in 0 ..< self.elementCount {