Saturday, January 17, 2015

UIPickerView - One selection among multiple options


1. Drag a UIPickerView to the storyboard

2. Control-Drag the UIPickerView to the ViewController for datasource and delegate

3. Complete the code as below:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    let languageList = ["United States", "United Kingdom", "Australia", "China", "Hong Kong", "Taiwan"]
    let languageCodeList = ["en-us", "en-gb", "en-au", "zh-cn", "zh-hk", "zh-tw"]
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return languageList.count
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return languageList[row]
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        println("language code is \(languageCodeList[row])")

    }

Show/Hide the keyboard while using the UITextView/UITextField

To show the keyboard:


self.inputTextView.becomeFirstResponder()


To hide the keyboard:

self.inputTextView.resignFirstResponder()



To hide the keyboard while tapping outside the UITextField/UITextField:

@IBOutlet weak var inputTextView: UITextView!

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.inputTextView.resignFirstResponder()
    }


To hide the keyboard while tapping the return key in the UITextField:

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var inputTextField: UITextField!
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.inputTextField.delegate = self

Wednesday, January 14, 2015

UIScrollView - Zoom in/zoom out and scroll/move an image with ScrollView

1. Drag a UIScrollView to the storyboard.

2. Control-drag the UIScrollView to

    @IBOutlet weak var myUIScrollView: UIScrollView!


3. Complete the code as below:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var myUIScrollView: UIScrollView!
    
    var myUIImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.myUIScrollView.maximumZoomScale = 5.0
        self.myUIScrollView.minimumZoomScale = 0.5
        self.myUIScrollView.delegate = self
        
        myUIImageView = UIImageView(image: UIImage(named: "big_image.png"))
        self.myUIScrollView.addSubview(myUIImageView)
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return myUIImageView

    }

Tuesday, January 13, 2015

AVSpeechSynthesizer - Text-to-Speech (TTS) function

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mySynthesizer = AVSpeechSynthesizer()
        let myUtterence = AVSpeechUtterance(string: "Hello World. Testing 1 2 3.")
        myUtterence.rate = AVSpeechUtteranceMinimumSpeechRate
        myUtterence.voice = AVSpeechSynthesisVoice(language: "en-au")
        myUtterence.pitchMultiplier = 0.5 //between 0.5 and 2.0. Default is 1.0.
        mySynthesizer.speakUtterance(myUtterence)

    }

Monday, January 5, 2015

SLComposeViewController - Post a message and an image to Facebook

Update:
December 19, 2015 - The solution below no longer works. It seems that Facebook SDK is required.
January 15, 2016 - See this: Facebook SDK and Swift - Post a message and an image to Facebook

1. Create a UIButton and a UIImageView

2. Include an image file swift.png (or a photo) in the project

3. Write the code as below:


import UIKit
import Social

class ViewController: UIViewController {

    @IBOutlet weak var myImage: UIImageView!
    
    var myUIImage : UIImage!
    
    @IBAction func facebookButtonPressed(sender: UIButton) {
        if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
            var myFBController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            myFBController.setInitialText("Test http://studyswift.blogspot.com")
            myFBController.addImage(self.myUIImage)
            self.presentViewController(myFBController, animated: true, completion: nil)
        } else {
            let alertView = UIAlertView()
            alertView.message = "Please login to Facebook."
            alertView.addButtonWithTitle("OK")
            
            alertView.show()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myUIImage = UIImage(named: "swift.png")
        
        self.myImage.image = myUIImage

Facebook SDK and Swift - Create a Facebook Login Button
Facebook SDK and Swift - Post a message and an image to Facebook
Facebook SDK and Swift - Post a message using Graph API and post an image using FBSDKShareKit