Hello ! Happy new year !
Superb article on how to use functions cleverly in Swift. Hope you’ll like it 🙂
https://medium.com/@johnsundell/first-class-functions-in-swift-32142962022e
Cheers,
MAB
Hello ! Happy new year !
Superb article on how to use functions cleverly in Swift. Hope you’ll like it 🙂
https://medium.com/@johnsundell/first-class-functions-in-swift-32142962022e
Cheers,
MAB
The official MABDEV Xcode color scheme !
What I tried to achieve:
You’ll need the Consolas Font for a better result.
MABDEV Xcode Color scheme download
Hope you like it !
Cheers,
MAB
Hi !
This guy just save me the trouble of writing a post about writing to the iPhone filesystem. Crystal clear explanations.
Writing files using NSDocumentDirectory in Swift
Cheers,
MAB
Hi ! I was recently asked to make the iPhone vibrate while using the app (not by notification). It’s been a while since I had to do it and back then it was in Obj-C. Here’s how to achieve it in Swift:
1 2 3 4 5 6 7 8 9 10 |
import UIKit import AudioToolbox class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) } } |
Cheers,
MAB
Hello ! I know its been a while, holidays and all …
Here’s a little extension that does no magic but shortens your code if you have to load child view controller from storyboards. Sorry for the lack of syntax colouring, WordPress does not seem to support Swift yet.
[code language=”text”]
import Foundation
typealias ViewControllerStoryboardInfo = (name: String, id: String?)
protocol Container {
}
extension Container where Self: UIViewController {
func addChildViewController(withInfo info: ViewControllerStoryboardInfo)-> UIViewController? {
var viewController: UIViewController! = nil
switch info.id {
case let identifier where identifier != nil:
viewController = UIStoryboard(name: info.name, bundle: nil).instantiateViewController(withIdentifier: identifier!)
default:
viewController = UIStoryboard(name: info.name, bundle: nil).instantiateInitialViewController()
}
if let viewController = viewController {
self.addChildViewController(viewController)
viewController.view.frame = self.view.bounds
self.view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
return viewController
}
return nil
}
func removeAllChildViewController() {
self.childViewControllers.forEach({ (controller) in
controller.view.removeFromSuperview()
controller.willMove(toParentViewController: nil)
controller.removeFromParentViewController()
})
}
}
[/code]
Cheers,
MAB
Hey !
I know its been a while, I am qui busy at Moment Factory right now. Those guys are geniuses. Anyway, I wanted to share with you, if you don’t already know it, how to animate anything that uses a UIImage aka. UIImageView, UIButton, etc…
To create an animated UIImage, include in you project a folder that contains your animation with a PNG for each frame. The files needs to be named like this:
I strongly recommend not to include these in an asset file, if you try you’ll quickly understand why.
Then you create a UIImage :
1 |
let animatedImage = UIImage.animatedImageNamed("WhatEverNameYouWant-", duration: 5) |
You can the use it with a UIButton like you would with any other UIImage :
1 |
button1.setImage(animatedImage, forState: .Normal) |
That’s it ! Have fun animating you apps !!
MAB
Hi all,
Not much this week except this
Superb article from Matt Gallagher that explains a lot on long compile time in swift.
Happy reading !
MAB
Let’s say you want to configure some parts of your view rendering directly in your storyboards. Fortunately you can by using @IBInspectable.
1 2 |
@IBInspectable var radius: CGFloat = 0 @IBInspectable var strokeColor: UIColor = UIColor.black() |
Here is the project ( Xcode 8, Swift 3) if you want to try it : IBInspectableDemo
That’s it folks, have a good day !
MAB
I recently had a problem with a yellow label on a dynamic background that can sometimes also be yellow. The two common solutions are
NSAttributedString offers everything you need to do it.
1 2 3 4 5 6 7 8 9 10 |
var outlinedLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 50)) let strokeTextAttributes = [ NSStrokeColorAttributeName : UIColor.blackColor(), NSForegroundColorAttributeName : UIColor.lightGrayColor(), NSStrokeWidthAttributeName : -4.0, NSFontAttributeName : UIFont.boldSystemFontOfSize(52) ] outlinedLabel.attributedText = NSAttributedString(string: "Outlined", attributes: strokeTextAttributes) |
Note that the stroke width attribute is a negative value.
1 |
NSStrokeWidthAttributeName : -4.0 |
This is to tell the drawing engine it is a “Stroke & Fill” operation. Otherwise it’ll just ignore the “NSForegroundColorAttributeName” and leave the text transparent like this :
Link to the playground : MABDev.OutlinedText.playground
Documentation : https://developer.apple.com/library/mac/qa/qa1531/_index.html
Cheers,
MAB