Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
woodycatliu / PublishersCombine.swift
Last active August 29, 2022 08:23
Combine Publishers Zip
//
// ViewController.swift
// CombineCombine
//
// Created by Woody Liu on 2021/8/16.
//
import UIKit
import Combine
@woodycatliu
woodycatliu / OptionalNotified.swift
Last active February 15, 2022 01:58
Observing optional value change allow NoticationCenter post
//
// OptionalNotified.swift
//
// Created by Woody Liu on 2021/7/24.
//
import Foundation
@propertyWrapper
public class OptionalNotified<Value: Equatable> {
@woodycatliu
woodycatliu / Notified.swift
Created July 24, 2021 05:11
這是從 https://github.com/ailabstw/social-distancing-ios 搬運出來的 Observed 屬性包裝器
import Foundation
@propertyWrapper
public class Notified<Value: Equatable> {
public let notificationName: Notification.Name
public var wrappedValue: Value {
didSet {
if oldValue != wrappedValue {
import Foundation
@propertyWrapper
public class Observed<T> {
private var _value: T {
didSet {
execute()
}
}
import Foundation
@propertyWrapper
public class Observed<T> {
private var _value: T {
didSet {
execute()
}
}
/* 設定Label 文字漸層色
有兩三種做法,這個作法是我最喜歡的,不需要考慮到 view 的生命週期。
原理: 在label 繪製text之前。建立一個漸層色image,再將它轉成顏色給textColor
*/
extension UIImage {
extension UIColor {
class var mainXXX: UIColor {
let color = UIColor(XXX)
return color
}
static let main: UIColor = {
/*
compressImgMid: 二分壓縮法,指定預期的 MB
imgWithNewSize: 更改 img 尺寸,可依照預期 imgView 的尺寸去縮放(一般縮到跟 imgView 預期最大即可)
*/
extension UIImage {
extension NSMutableAttributedString {
func insertImage(image: UIImage?, tintColor: UIColor? = nil, bounds: CGRect = .zero, at: Int) {
let imageAttachment = NSTextAttachment()
if let tintColor = tintColor{
// 搭配自己的UIImageExtension
imageAttachment.image = image?.tinted(color: tintColor)
} else {
imageAttachment.image = image
}