Skip to content

Instantly share code, notes, and snippets.

@soggybag
soggybag / SendSMSViewController.swift
Last active August 28, 2017 15:13
Send SMS Messages with Swift
import UIKit
import MessageUI // Import MessageUI
// Add the delegate protocol
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
// Send a message
func sendMessage() {
let messageVC = MFMessageComposeViewController()
messageVC.body = "Message String"
@soggybag
soggybag / SendEmailViewController.swift
Last active October 17, 2015 06:00
Send Email with Swift
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
func sendEmail() {
let mailVC = MFMailComposeViewController()
mailVC.mailComposeDelegate = self
mailVC.setToRecipients([])
@soggybag
soggybag / Swift Attributed String
Created October 17, 2015 06:16
Swift Attributes String Extension
extension String {
func stringToAttributedString() -> NSAttributedString {
var html = self
while let range = html.rangeOfString("\n") {
html.replaceRange(range, with: "</br>")
}
html = "<span style='font-family: Helvetica; font-size:14pt'>"+html+"</span>"
let data = html.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)
let attrStr = try! NSAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
@soggybag
soggybag / Swift Array Safe Extension
Created October 17, 2015 15:49
Returns element or nil for index in array if there is an element at index
extension Array {
subscript (safe index: Int) -> Element? {
return indices ~= index ? self[index] : nil
}
}
// array[safe: index]
@soggybag
soggybag / Extensions.swift
Last active August 2, 2016 20:27
Swift UIView Extension - Animation helper
import UIKit
extension UIViewController {
func animateThing(thing: UIView, offsetX: CGFloat, offsetY: CGFloat, alpha: CGFloat, time: NSTimeInterval, delay: NSTimeInterval) {
let targetY = thing.center.y
let targetX = thing.center.x
let targetAlpha = thing.alpha
thing.center.y = thing.center.y - offsetY
@soggybag
soggybag / Example.swift
Last active November 6, 2015 15:12
Enum Example, stores colors used by your app.
// Create a color
var myColor = TagColor.Red
// or, set a var to type TagColor
var aColor: TagColor
// Assign a TagColor
aColor = TagColor.Blue
// Alternately use the shorthand form
aColor = .Blue
@soggybag
soggybag / TagColor.swift
Created November 6, 2015 15:17
Enum example TagColor step 2
enum TagColor {
case Red, Orange, Yellow, Green, Blue, Purple
func toUIColor() -> UIColor {
switch self {
case .Red:
return UIColor(red: 179, green: 47, blue: 60, alpha: 1)
case .Orange:
return UIColor(red: 248, green: 148, blue: 29, alpha: 1)
case .Yellow:
@soggybag
soggybag / ViewController.swift
Created November 8, 2015 18:04
Minimal ViewController using Image Picker to take a picture with the camera and display it in an image view.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// Must be declared here!
var picker = UIImagePickerController()
// IBOutlets
@soggybag
soggybag / sample.swift
Created November 8, 2015 23:14
NSNumberFormatter Example
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
let costString = formatter.stringFromNumber(cost)
@soggybag
soggybag / sample.swift
Created November 9, 2015 03:40
More examples with NSNumberFormatter
// Currency style
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
let costString = formatter.stringFromNumber(23.99)
// Round to nearest whole number
let wholeNumberformatter = NSNumberFormatter()
let wholeNumber = wholeNumberformatter.stringFromNumber(3.145)
// Round to nearest whole number