Skip to content

Instantly share code, notes, and snippets.

Avatar
🌐
Woohoooo 12k on SO!

Andrew Zheng aheze

🌐
Woohoooo 12k on SO!
View GitHub Profile
View AdaptiveCollectionLayoutDelegate.swift
let item = provider.items[indexPath.row] as! MyViewModel
let textHeight = item.title.count
let extensionHeight = Double(textHeight) * 0.70
return AdaptiveCollectionConfig.cellBaseHeight + CGFloat(textHeight) + CGFloat(extensionHeight)
View AdaptiveCollectionLayoutDelegate.swift
extension String {
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: font], context: nil)
return boundingBox.height
}
}
View AdaptiveCollectionLayoutDelegate.swift
let item = provider.items[indexPath.row] as! MyViewModel
let textHeight = item.title.heightWithConstrainedWidth(width: width-24, font: UIFont.systemFont(ofSize: 10))
//Minus 24 points because that is the padding of the edges of the label
let extensionHeight = Double(textHeight) * 0.70
return AdaptiveCollectionConfig.cellBaseHeight + CGFloat(textHeight) + CGFloat(extensionHeight)
View DeleteAndInsert.swift
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == baseCollectionView {
let alphabetLetter = baseCollectionViewArray[indexPath.item]
secondCollectionViewArray.append(alphabetLetter)
let newIndexPath = IndexPath(item: secondCollectionViewArray.count - 1, section: 0)
secondCollectionView.insertItems(at: [newIndexPath])
///First, add a cell to the second collectionview.
baseCollectionViewArray.remove(at: indexPath.item)
baseCollectionView.deleteItems(at: [indexPath])
View AlphabetComponent.swift
class AlphabetComponent: NSObject {
var labelName = ""
var orderIdentifier = 0
}
View NewComponentArray.swift
class MyViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
///.......
override func loadView() {
let view = UIView()
view.backgroundColor = .white
///ADD this stuff:
for (index, singleLetter) in baseCollectionViewArray.enumerated() {
let newComponent = AlphabetComponent()
View FinalOrderedCollectionview.swift
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class AlphabetComponent: NSObject {
var labelName = ""
var orderIdentifier = 0
}
View OrderedCV_Step1.swift
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
///1.
var baseCollectionViewArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
var secondCollectionViewArray = [String]()
View convertDateToReadableStringFunction.swift
func convertDateToReadableString(dateToConvert: Date) -> String {
/// Make a date formatter object. This will take care converting Date objects to String objects and vice versa.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMddyy" /// This is a pretty good format that will yield something like "January 1, 2020"
let todaysDate = Date() /// When you initialize a Date object like this, the Date will be the current date.
let todaysDateAsString = dateFormatter.string(from: todaysDate)
let yesterday = todaysDate.subtract(days: 1)
let yesterdaysDateAsString = dateFormatter.string(from: yesterday!)
let oneWeekAgo = todaysDate.subtract(days: 7)
View FixTodoStep2.swift
for index in indicies {
...
/// ↓ Delete this ↓
/// print("\"\(originalFoundText)\" | Position: x: \(finalX), y: \(finalY), width: \(finalWidth), height: \(finalHeight)")
/// Todo: Step 2... place the rectangle "in real life"
/// ↓ Add this ↓
let rect = CGRect(x: finalX, y: finalY, width: finalWidth, height: finalHeight)
rectangles.append(rect)