Skip to content

Instantly share code, notes, and snippets.

Josh Marasigan josh-marasigan

View GitHub Profile
View FrameExample.swift
let frame1 = CGRect(x: 20, y: 250, width: 157.5, height: 157.5)
let view1 = UIView(frame: frame1)
let frame2 = CGRect(x: 197.5, y: 250, width: 157.5, height: 157.5)
let view2 = UIView(frame: frame2)
View SnapKitExample.swift
let view1 = UIView()
view1.backgroundColor = UIColor.systemIndigo
view.addSubview(view1)
view1.snp.makeConstraints { make in
make.top.equalToSuperview().offset(250)
make.leading.equalToSuperview().offset(20)
make.width.height.equalTo(view.snp.width)
View LayoutConstraintExample.swift
NSLayoutConstraint(item: view1, attribute: .leading,
relatedBy: .equal,
toItem: view, attribute: .leading,
multiplier: 1.0,
constant: 20).isActive = true
View AnchorConstraintsExample.swift
let view1 = UIView()
view1.backgroundColor = UIColor.systemRed
view1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(view1)
view1.topAnchor.constraint(equalTo: view.topAnchor,
constant: 250).isActive = true
view1.leadingAnchor.constraint(equalTo: view.leadingAnchor,
@josh-marasigan
josh-marasigan / AddStringAttributeToFoundTextRange.swift
Created May 8, 2020
`NSMutableAttributedString` extension for adding attributes to found ranges of indicated text within an `NSAttributedString`/`NSMutableAttributedString`
View AddStringAttributeToFoundTextRange.swift
/*
ex.) The following adds an underline attribute to "an example" within "This is an example".
let string = NSMutableAttributedString(string: "This is an example.")
let underlineAttributes: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]
string.addAttributeToRange(in: "an example", attributes: underlineAttributes)
View ViewController.swift
// `.` indicates code left out.
// Please refer to the sample app for reference
// https://github.com/josh-marasigan/ObserverDesignExample
class ViewController: UIViewController {
.
.
.
// MARK: - Button Configuration
private func configChangeBackgroundButtonUI() {
.
View TitleAndTextView.swift
// `.` indicates code left out.
// Please refer to the sample app for reference
// https://github.com/josh-marasigan/ObserverDesignExample
// Somewhere in the TitleAndTextView.swift file
class TitleAndTextView: UIView {
.
.
.
// MARK: - Class Properties. Fun fact: emojis are just stirng literals
View ObserverKeys.swift
struct ObserverKeys {
static let teal = "observer-example.teal"
static let pink = "observer-example.magenta"
}
@josh-marasigan
josh-marasigan / SwiftMapExercise.swift
Last active Apr 28, 2018
Varying Swift syntax for map function
View SwiftMapExercise.swift
// Most verbose: No compiler assumptions are made, the closure to map
// receives a transforming function explicitly casted as (val: Int) -> Int
// Transforms an array of Int and multiplies each value by 10
arrValues.map({ (val: Int) -> Int in
return val * 10
})
// Compiler assumes return type for closure, no need to explicitly indicate
arrValues.map({ (val: Int) in
return val * 10
View DelegateRuntimeExample.swift
let josh = Son()
let joshsMom = Mom(son: josh)
joshsMom.tellJoshWhatToDo()
---
"Washing Dishes"
"Cleaning Room"
"Walking Nala"
You can’t perform that action at this time.