It's been some time since last blog post. It's been quite a busy last couple of weeks, so I have been concentrating on the final app project - The App Academy Code Challenge.
Today is the last day of our course, and we are presenting our Code Challenge app. We are happy with the result so far and looking I wanted to share with you what we have so far at least.
0 Comments
Attributed String and Regular Expressions (RegEx). Full Stop. These two things have been occupying my mind for the first 3 weeks of our final project at iOS Bootcamp with The App Academy, and its been a love hate relationship. I will admit mostly hate in the beginning, but eventually (like most things) it becomes more clear the more you work on it. This is a too short article to explain this in full. Hopefully I can make a nice tutorial soon.
So what do I mean when I say: how to make text look good? Take an example of a ToDo-list:
Displaying this in with a String in a Label is not so difficult of course. But what if you want to make this ToDo-list visualise the items on it differently, for example like this:
This is a bit trickier to display in a Label with a String. This is where attributed strings comes in. An attributed string is a string can hold attributes for individual words and characters. With attributes I mean font size, background color, font color, font type, underlining etc. The easy recipe for doing this is as follows:
Doesn’t sound too difficult right? And it doesn’t have to be if you are only attributed one or two string. Then you can manually set the different attributes to the different ranges as you want. But if you want to automate this process so you can use it with multiple string, then it gets more complicated. For example if you want every word that is surrounded by *’s, for example *Get a job*, to be red and bold, then you need to have a way of searching the string for those *. This is where regular expressions come in. Regular expressions is a sequence of symbols and characters expressing a string or pattern to be searched for within a piece of text. This is too complicated to explain in one small article, but I’ll post some links to some good tutorials and websites that explains it better. Basically you can say use an expression that searches for *Important* then adds an attribute to that search result. The regular expression for this would be simply (*Important*), but this is pretty easy. Other examples of regular expressions are:
So you can see it can become quite complex, but there is also so much you can do with them. So the easy recipe from before now becomes a bit more advanced:
Instead of adding attributes to the matches you can also for example search and replace word within a string. For some more explanation about this look at these websites: http://www.regexpal.com/ is an easy tool to test out regular expressions to see if you can match what you are looking for. NSRegularExpression Tutorial and Cheat Sheet and NSRegularExpression Tutorial: Getting Started at Ray Wenderlich is a good place to start to learn about attributed strings and regular expressions
PHAsset is a class for fetching metadata about media that is located in your Photo Library. So if you are making an app to pin point all the locations of your favourite photos on a map, then you would use PHAsset to extract this data.
PHAsset is part of the Photos framework, which replaced AssetsLibrary from iOS 9.0 onwards. So remember to import Photos to your code. The first thing you need to do is to fetch the asset or assets. You can choose between either collecting one asset (photo or video), an collection of them, all assets of a certain media type or a media with a certain identifier or URL. It is important to remember that this class won’t fetch the actual media (photo, video, audio), but the metadata of the media. After you them fetched the assets you can either gather metadata about them to use or you can also edit the metadata or the assets. So what data can you actually get after you have fetched the asset(s). Here is a quick list:
I previously wrote a tutorial on “How to read iOS Documentation” and there I used an example with fetching location and modificationDate from a older photo. Take a look at that here and see how you can fetch the metadata of an asset. I’ll also copy in a screenshot of my code that I used. This assumes that you have already chosen if you want to grab the picture from the Photo Library or if you want to take a new picture (hence the if else statement). It's sunny in Amsterdam and like any respectable Amsterdammer (and Norwegian) I am enjoying the sun to the fullest.Today that involves:
When I first started thinking of being a programmer this was part of the lure. If its nice weather, take the laptop outside for a few hours to change the environment for a little bit. This was especially tempting while sitting in my old office at NAM in Assen. Only missing now is a cold beer towards the end of the day to cheers the new lifestyle! |
Archives
April 2016
Categories
All
|