Thứ Năm, 4 tháng 9, 2014

Understanding Self Sizing Cells and Dynamic Type in iOS 8

4 Flares 4 Flares ×

In iOS 8, Apple introduces a new feature for UITableView known as Self Sizing Cells. To me, this is seriously one of the most exciting features for the new SDK. Prior to iOS 8, if you want to display dynamic content in table view with variable height, you would need to calculate the row height manually. Now with iOS 8, Self Sizing Cell provides a solution for displaying dynamic content. In brief, here are what you need to do when using self sizing cells:

  • Define auto layout constraints for your prototype cell
  • Specify the estimatedRowHeight of your table view
  • Set the rowHeight of your table view to UITableViewAutomaticDimension

If we express the last two points in code, it looks like this:

1
2
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

With just two lines of code, you instruct the table view to calculate the cell’s size matching its content and render it dynamically. This self sizing cell feature should save you tons of code and time. You’re gonna love it.

self-sizing-cell-featured

Building a Simple Demo Using Self Sizing Cell

There is no better way to learn a new feature than using it. We’ll develop a simple demo app to demonstrate self sizing cell. We’ll start with a project template. It’s just a simple table-based app showing a list of hotels. The prototype cell contains two one-line text labels for both name and address. If you download the project and run it, you’ll have an app like below:

Table View App with Truncated Text

As you can see, due to fixed row height, some of the hotel addresses are truncated. You may have faced the same issue when developing table-based apps. In the past, you may simply reduce the font size or increase the number of lines to fix the issue. Starting from iOS 8, all you need to do is to use Self Sizing Cells and the cell content can be displayed properly, regardless of content length.

Adding Auto Layout Constraints

You may hate Auto Layout and avoid using it. However, without auto layout, self sizing cells won’t work as it relies on the constraints to determine the proper row height. In fact, table view calls systemLayoutSizeFittingSize on your cell and that returns the size of the cell based on the layout constraints.

If this is the first time you work with Auto Layout, I recommend you to check out our Auto Layout Introduction before moving on.

For the project template, I haven’t defined any auto layout constraints for the prototype cell. So let’s add a few constraints for the cell first. For the name label, click the Pin button of the auto layout menu and add four spacing constraints.

Auto Layout Prototype Cell

For the address label, add further three spacing constraints for the left/right/bottom side.

Auto Layout Prototype Cell

If you have configured the constraints correctly, they should look something like below:

Self Sizing Cell - Auto Layout

Self Sizing Cell - Auto Layout

Setting Estimated Row Height

With the auto layout configured, the rest is to add the following code in the viewDidLoad method of the ViewController:

1
2
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension

The first line of code sets the estimated row height of the cell, which is the height of the existing prototype cell. The second line changes the rowHeight property to UITableViewAutomaticDimension, which is the default row height in iOS 8. In other words, you tell table view to figure out the cell size based on other information.

If you test the app, the cell is still not resized. The reason is that both name and address labels are set to 1-line. So set the number of lines to zero and let the label grow automatically.

Self Sizing Cell - Number of Line

Now compile and run the app again. The table view cells are resized according to the content.

Self Sizing Cell Demo

A Bug?!

I’m not sure if it is a bug or not. But the problem was also mentioned by UseYourLoaf. When the table view is first displayed, you may find some of the cells are not sized properly. But when you scroll the table view, the new cells are displayed with correct row height. To workaround this issue, you can force a reload after the view appears:

1
2
3
4
5
    override func viewDidAppear (animated : Bool ) {
   
        tableView.reloadData ( )
   
    }

Dynamic Type

Self Sizing Cell is particularly useful to support Dynamic Type. You may not have heard of Dynamic Type but you probably see the following screen in Settings:

Dynamic Type Settings

Dynamic Type was introduced in iOS 7. It allows users to customise the text size to fit their own needs. However, only apps that adopt Dynamic Type respond to the text change. I believe only a fraction of third-party apps have adopted the feature.

Starting from iOS 8, Apple wants to encourage developers to adopt Dynamic Type. As mentioned in the WWDC session, all of the system applications have adopted Dynamic Type and the built-in labels have dynamic fonts on them automatically in iOS 8. As the user changes the text size, those labels are going to change size.

Further, the introduction of Self Sizing Cell is a way to facilitate Dynamic Type adoption. It saves you tons of code from developing your own solution to adjust the row height. Once the cell is self-sized, it is very straightforward to adopt Dynamic Type.

In the demo project, you just need to change the font from a custom font of fixed size to a preferred font for text style (e.g. headline, body, etc). That’s it. When you run the app, it adapts to the text size change.

Dynamic Type Demo

Summary

In this tutorial, I have walked you through the basics of Self Sizing Cells and Dynamic Type. You’re encouraged to use the feature and update your app to adopt Dynamic Type. Self Sizing Cell is seriously one of my favorite features in iOS 8. With just a couple lines of code, you can easily fit dynamic content in a cell. This is truly fabulous. What do you think? Leave me comment and share your thought.

For your reference, you can download the complete project from here. Please note that I used Xcode 6 Beta 7 to create the project.

If you enjoy this tutorial, you’ll probably like our new book. We’re going to launch a new book called Beginning iOS 8 programming with Swift. It’s a beginner guide for anyone who want to learn Swift, Xcode 6 and master the new APIs of iOS 8 SDK. The book will launch in October. To receive an early access of the book, please sign up here.
Source : appcoda[dot]com

Create a New iTunes Radio Station from Currently Playing Song or Artist

iTunes Radio Music Have a favorite song that you want to use to seed a new music station? Maybe you’re looking to find some new tunes to hear based on a particular song or artist? iTunes Radio makes this process simple, and you can instantly create a new iTunes Radio station from any playing song or artist, whether that song is in your music library or playing from an existing iTunes Radio station. The latter situation is fairly well known by Radio users, but creating a new station from songs that already exist in an iTunes playlist is less well known. This works with any iTunes client that supports the Radio feature, whether it’s the desktop iTunes app or the Music app in iOS.


iTunes Radio is currently region limited, but for users who are outside of the USA and other areas with Radio support, you can still listen to iTunes Radio with a US-based Apple ID.

How to Make a New iTunes Radio Station from a Playing Song in iTunes

Creating a new radio station this way is the same in Mac OS X and Windows:

  1. From the iTunes app, access your music playlist or library as usual
  2. Hover over a song then click the (>) arrow button over the song name
  3. Select “New station from Artist” or “New station from Song” to create a new iTunes Radio channel

Create a new iTunes Radio station from a song or artist

You can also access the New Station features from the iTunes Album Art player, as shown above, or with a Right-Click on any song name in the iTunes song playlist.

Making a new radio station this way will immediately jump to the Radio portion of iTunes (or Music app), from there you can either tweak it to play towards discovering new music or just playing the hits, to allow or deny explicit lyrics and thus album versions of some songs, and the other usual adjustments.
Playing a newly created iTunes Radio station

On the iOS side of things, you can access the same feature within the Music app on the iPhone, iPad, or iPod touch, by tapping on the (i) button as if you were to share a station but choose “New station from song” or “New station from artist” instead.

Enjoy your newly created iTunes station. This can be a really great way to find new music, particularly if you tweak the settings for “Discovery”, or if you just want to play some of the related genre classics, keep it on ‘Hits’, which is the default setting.

Source : osxdaily[dot]com

Thứ Ba, 2 tháng 9, 2014

How to Remove Red Eye from Photos on iPhone & iPad

Fix Red Eye on iPhone with Photos app

Red Eye happens sometimes in photography, often if you’re shooting a subject while using the camera flash, or when a bright light is shining in their eyes. The appearance is striking and usually undesirable, with the subjects eyes literally glowing red. You may run into the red eye effect when taking pictures with any camera, but the iPhone, iPod touch, and iPad all have a fantastic trick up their sleeve that lets you quickly remove red eye from just about any picture.


To use the native red eye removal tool, you’ll need to be running modern version of iOS versioned anywhere at or above 7.0, other than a newish versions of iOS, no additional downloads or apps are needed. The feature is part of the editing tools in the Photos app, and though you can certainly use it to remove red eye from any picture taken with the iPhone camera, you can also use the same tool on any picture provided to the iOS device too.

How to Remove Red Eye Effect from Pictures with iPhone, iPad, & iPod touch

  1. Go to the Photos app and tap on the picture that has the red eye effect you wish to fix
  2. Tap on the photo and then tap on the “Edit” button
  3. Tap the little eye icon with a slash through it (this is the red eye removal tool button)
  4. Remove Red Eye from photos with iPhone

  5. Tap directly onto the red eyes in the photo you want to fix and remove, choose one at a time
  6. When finished correcting all red-eye and satisfied with the result, tap on “Apply” to save the change
  7. Fixing red eye photos on the iPhone

The results of the iOS Photos app native Red Eye Removal Tool are immediate and extremely effective, as shown in this example:

Red eye removal on iPhone before and after picture

If you want to try out the red eye removal tool yourself but don’t have a picture handy, you can use the same red eye sample image we are in the tutorial courtesy of Wikipedia here. The feature works the same on any iPhone, iPad, or iPod touch.

Note that you can undo the red eye removal at any time as long as the originally edited picture is stored on the iOS device, this is much like applying filters that can also be later removed if desired.

This is really an excellent solution that Apple created, and it doesn’t require any funky tricks, photoshop usage, or third party apps. It’s also preferable to turning the picture into black and white, which is sort of an old fashioned way of handling red eyed images, and with how easy and effect the iOS native red eye tool works, you should aim to use it first.

Source : osxdaily[dot]com

Thứ Hai, 1 tháng 9, 2014

Change the Number of Apps in the iOS Dock to Get Minimal

Customize the iOS Dock and change it

The iOS Dock sits at the bottom of our iPhone, ipod touch, and iPad home screen, intended to hold the most commonly used apps for quicker launching. While it’s well known that you can customize the apps that are contained within the Dock, what’s lesser known is that you can actually reduce the number of apps visible from the 4 default, down to 3, 2, 1, and, if you really want to, 0 apps.

Reducing the app count in the iOS Dock is done by simply by pulling the apps out of the Dock. First, tap and hold on any app icon to make the home screen icons jiggle, then once when they’re wiggling around, drag the app(s) from the Dock back onto the home screen that you want to remove from the Dock.

Here’s what the iPhone Dock look like with just a single app visible, the Phone app:

Minimalist iPhone dock

Reducing the Dock app count to a single or couple of apps may be practical for some situations, but it’s hard to imagine a reason to do clear everything from the Dock… nonetheless I actually had a friend ask to do just that… so, if for whatever reason you’re not a fan of having apps in the Dock or maybe you just want a clean slate, you can yank everything out out and start with an empty Dock.

Removing everything from the iOS Dock is done the same way you’d move icons around in iOS in general: Again, simply tap and hold on a Dock icon to start it jiggling, then move each app icon from the Dock and onto the Home Screen. Repeat until it’s totally empty, and you’re left with an empty Dock. This is what this looks like with a home screen still full of icons:

Empty iPhone Dock

Having nothing in the Dock is really a waste of valuable screen space, because unlike in OS X, it’s not like the iOS Dock hides itself when it’s not in use. Instead, it’ll just take up a considerable portion of your home screen without serving a purpose, making this a rather pointless exercise, but yes, it can be done if you want it done for some reason or another.

If you’re aiming for minimalism, a much more practical endeavor may be to create a completely blank home screen instead, which emphasizes the wallpaper. This is my personal preference, and I use it to keep a home screen that is free of any icons until you flip over to see them. For users like me who mostly use the apps in their Dock icons, this can work without interfering in productivity:

blank iPhone home screen to emphasize the wallpaper

Combine the above trick with reducing the total number of icons in the Dock to a lower number and you may be able to find a happy medium too, particularly if you feel like the default amount of 4 for the iPhone and 6 for the iPad is too busy. But going with absolutely nothing in the Dock or the initial home screen? Well, that’s possible, but as you can see, it looks quite funny, you are left with literally a iOS blank screen of nothing:

blank dock blank home screen on iPhone

Perhaps it would look better with a custom wallpaper, but again, it’s hard to imagine a scenario where it would be practical. Regardless, it’s nice to know that iOS has that level of customization, able to suit whatever your dock needs are. Don’t forget that you can also change the Dock appearance by adjusting the transparency settings or changing the wallpaper used too.

Source : osxdaily[dot]com