Search

stitchzaa

Some useful code & information for web dev (HTML5, CSS3, JavaScript) and iOS app dev (Swift)

Set environment for Angular + node.js + exist repository project from zero

– Register Bitbucket then import (or create a new one).
– Install SourceTree for controlling repository.
– Check out repo in SourceTree.
– Install node.js
– Install Git
– open cmd and go to target ‘directory’.
> npm install
> bower install
> npm install -g gulp
> npm start
– open another cmd and go to target ‘directory’.
> gulp watch

jQuery: new element and insert element.

How to create a new element in jQuery:

var unreadMarker = $('<a>', {
  "class": "unread-marker"
});

OR

var unreadMarker = $('<a class="unread-marker"></a>');

How to insert element:

a.after(b) ==>  <a>  <b>

a.insertAfer(b) ==> <b> <a>

a.append(b) ==> <a> xx yy b </a>

a.prepend(b) ==> <a> b xx yy </a>

Use regular expression to filter telephone number in JavaScript

function checkPhoneNumber(phone) {
    var regexp = /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i
    if (regexp.test(phone) && phone.length > 0) {
        return true;
    } else {
        return false;
    }
}

Continue reading “Use regular expression to filter telephone number in JavaScript”

Import image from photo library into app in swift

Add  UIImagePickerControllerDelegate, UINavigationControllerDelegate  at top of the class.

 

Before viewDidLoad()

var picker = UIImagePickerController()

 

In viewDidLoad() or any function that you want to use image picker

picker.delegate = self

 

Add some function for catch event

// What to do when the picker returns with a photo
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
    dismissViewControllerAnimated(true, completion: nil)
    addAvatar(chosenImage)
//Use image that user pick from library
}

// What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: {() in
                // some code for do anything when user cancel to import image
    })
}

UIScrollView in Swift

Code for create UIScrollView (which named scrollView) and specify size of content (width and height) inside scrollView.

var scrollView = UIScrollView(frame: containerView.frame)

scrollView.contentSize = CGSize(width: 3000, height: 2000)

containerView.addSubview(scrollView)

// can be any view that is container of scrollView


Continue reading “UIScrollView in Swift”

Data-binding

Link of information about Data-binding Revolutions with Object.observe()

http://www.html5rocks.com/en/tutorials/es7/observe/

Material icons from Google

Material flat icons for web, Android, and iOS projects.

https://www.google.com/design/icons/

How to integrate MZFormSheetPresentationController into swift project

MZFormSheetPresentationController is an ios framework that provide a popup from UIViewController.

example of MZFormSheetPresentationController
example of MZFormSheetPresentationController

Step to integrate MZFormSheetPresentationController

1. add these 2 lines to podfile and compile
pod 'MZAppearance'
pod 'JGMethodSwizzler'

2. download .zip file from MZFormSheetPresentationController and extract
Screen Shot 2015-08-02 at 5.41.08 AM

3. drag “MZFormSheetPresentationController” folder to your project
Screen Shot 2015-08-02 at 5.41.41 AM

4. in your Bridging Header file, add these 2 lines
#import "MZFormSheetPresentationController.h"
#import "MZFormSheetPresentationControllerSegue.h"

5. create your new UIViewController .xib file, name it (Ex: PopupViewController.xib) and create your own popup layout.
Screen Shot 2015-08-02 at 6.07.59 AM

6. Add these code in the UIViewController that you want to add your popup

class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var btn:UIButton = UIButton.buttonWithType(UIButtonType.InfoLight) as! UIButton
btn.addTarget(self, action: "btnInfoTouched", forControlEvents: UIControlEvents.TouchUpInside)
UIBarButtonItem(customView: btn)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btn)
}
func btnInfoTouched(){
var infoViewController = UIViewController(nibName: "PopupViewController", bundle: nil)
var formSheetController = MZFormSheetPresentationController(contentViewController: infoViewController)
formSheetController.contentViewSize = CGSizeMake(280, 70)
formSheetController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyle.StyleBounce
var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissPopup")
formSheetController.view.addGestureRecognizer(tap)
// add tap gesture recognizer to detect tap and close popup
self.presentViewController(formSheetController, animated: true, completion: nil)
}
func dismissPopup() {
self.dismissViewControllerAnimated(true, completion: nil)
}
}

7. build and see the result

explode and join array in Swift

how to join array to string

var arrayList:[String]!
…..
var newString:String = “-“.join(arrayList)

 &

how to explode string to array

var stringSentence:String!
…..
var
 newArray:[String] = stringSentence.componentsSeparatedByString(“-“)

Blog at WordPress.com.

Up ↑