Skip to content

Instantly share code, notes, and snippets.

View shrawan2015's full-sized avatar
🎯
Focusing

Shrawan Kr Sharma shrawan2015

🎯
Focusing
View GitHub Profile
@marcetcheverry
marcetcheverry / String.swift
Last active November 6, 2023 20:22
Convert HTML to Text/String in Swift
extension String {
/// Using regular expressions is not a correct approach for converting HTML to text, there are many pitfalls, like handling <style> and <script> tags. On platforms that support Foundation, one alternative is to use NSAttributedString's basic HTML support. Care must be taken to handle extraneous newlines and object replacement characters left over from the conversion process. It is a good idea to cache complex generated NSAttributedStrings either through storage or NSCache.
func strippingHTML() throws -> String? {
if isEmpty {
return nil
}
if let data = data(using: .utf8) {
let attributedString = try NSAttributedString(data: data,
options: [.documentType : NSAttributedString.DocumentType.html,
@jaredsinclair
jaredsinclair / NSString+HTML.swift
Created December 18, 2017 05:21
Convert HTML string to plain text (Swift version of MWFeedParser's utility)
/// Quick-n-dirty translation of MWFeedParser's algorithm from Objective-C to Swift
/// seealso: https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString%2BHTML.m
public extension NSString {
public func byConvertingHTMLToPlainText() -> String {
let stopCharacters = CharacterSet(charactersIn: "< \t\n\r\(0x0085)\(0x000C)\(0x2028)\(0x2029)")
let newLineAndWhitespaceCharacters = CharacterSet(charactersIn: " \t\n\r\(0x0085)\(0x000C)\(0x2028)\(0x2029)")
let tagNameCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
@fuxingloh
fuxingloh / UICollectionView.swift
Last active February 23, 2023 08:38
iOS Swift: How to count the width of the UILabel. And how to size UICollectionViewCell dynamically with label.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = collections[indexPath.row].name
let width = UILabel.textWidth(font: titleFont, text: text)
return CGSize(width: width + left + right, height: height)
}
@sheharyarn
sheharyarn / request.js
Last active August 24, 2023 14:55
Axios Request Wrapper for React (https://to.shyr.io/axios-requests)
/**
* Axios Request Wrapper
* ---------------------
*
* @author Sheharyar Naseer (@sheharyarn)
* @license MIT
*
*/
import axios from 'axios'
@vasanthk
vasanthk / System Design.md
Last active July 4, 2024 15:09
System Design Cheatsheet

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@onevcat
onevcat / nstimer_break_retain.swift
Created September 28, 2015 03:34
NSTimer extension which breaks the retain cycle in Swift.
private class Block<T> {
let f : T
init (_ f: T) { self.f = f }
}
extension NSTimer {
static func xxx_scheduledTimerWithTimeInterval(ti: NSTimeInterval, block: ()->(), repeats: Bool) -> NSTimer {
return self.scheduledTimerWithTimeInterval(ti, target:
self, selector: "xxx_blcokInvoke:", userInfo: Block(block), repeats: repeats)
}
@mwaterfall
mwaterfall / StringExtensionHTML.swift
Last active May 27, 2024 04:36
Decoding HTML Entities in Swift
// Very slightly adapted from http://stackoverflow.com/a/30141700/106244
// 99.99% Credit to Martin R!
// Mapping from XML/HTML character entity reference to character
// From http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
private let characterEntities : [String: Character] = [
// XML predefined entities:
"&quot;" : "\"",
"&amp;" : "&",