Skip to content

Instantly share code, notes, and snippets.

@enomoto
enomoto / displayScale.swift
Last active May 30, 2023 12:01
iOS display scale
import UIKit
print(UITraitCollection.current.displayScale) // 3.0 (if device is iPhone 14.0)
print(UIScreen.main.scale) // 3.0 (if device is iPhone 14.0)
var pixelLengthForUIKit: CGFloat = {
1 / UITraitCollection.current.displayScale
}
@enomoto
enomoto / ContentView.swift
Last active September 20, 2022 00:35
Sample for simple modifier
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
globeLabel
.padding()
.mainCell() // Applying quite simple custom modifier
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
globeLabel
.padding()
}
}
import csv
import requests
from bs4 import BeautifulSoup
def parse_collection_item(item):
h4 = item.find('h4')
event = item.find('li', class_='video-tag event').text
a = item.find('a', class_='video-image-link')
link_path = a.get('href')
url = 'https://developer.apple.com' + link_path
@enomoto
enomoto / ImageCache.swift
Created November 28, 2021 05:48
simple image cache for iOS
import UIKit
struct ImageCache {
static func loadImage(url: URL, user: User) async throws -> (UIImage, User) {
if let cachedImage = InMemoryImageCache.shared.get(forKey: url) {
return (cachedImage, user)
}
let urlRequest = URLRequest(url: url)
let (data, response) = try await URLSession.shared.data(for: urlRequest)
@enomoto
enomoto / StackViewInitialize.swift
Last active July 20, 2022 04:38
UIStackView initialization idiom
import UIKit
let stackView = UIStackView()
contentView.addSubview(stackView)
stackView.axis = .horizontal // axis's default value is horizontal.
// 以下2行はセット
stackView.isLayoutMarginsRelativeArrangement = true
stackView.directionalLayoutMargins = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
stackView.spacing = 6
@enomoto
enomoto / PlaygroundDrivenDevelopment1.swift
Last active May 9, 2019 08:23
Playground Driven Development (Xcode 10.2.1, Swift 5)
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
@enomoto
enomoto / RoundedCornersLabel.swift
Last active May 9, 2019 03:12
UILabel with rounded corners (Swift 5, Xcode 10.2)
@IBDesignable
public final class RoundedCornersLabel: UILabel {
public override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
configure()
@enomoto
enomoto / stride.swift
Last active April 7, 2019 12:35
replacement of C style for loop in Swift 5(Xcode 10.2)
// for var i = 0; i < 5; i += 1
for i in stride(from: 0, to: 5, by: 1) {
print(i) // 0, 1, 2, 3, 4
}
import Foundation
@testable import Foobar
import Quick
import Nimble
import Mockingjay
import APIKit
final class FooRequestSpec: QuickSpec {
override func spec() {
describe("FooRequest") {