Customize your texts in iOS using NSAttributedString

As I mentioned in my previous posts, I’m currently working on iOS platform and developing apps for it. As you know I’ve worked with variety of programming languages; but, there are some features in Objective-C which are unique! In fact, I haven’t seen such these features in other programming languages.

Assume you’re developing an iOS application and have added a UILabel to your view controller. Besides, you’ve linked the label to your ViewController.m class via an outlet named myLabel.

Now you can set the text of your label using its ‘text’ property or its [UILabel setText:] method. For example:

[myLabel setText: @"This is a text"];

The result would be a plain label with the “This is a text” text in it. But what if you want to have a text like this:

This is a text

The most easy way to achieve this is to have three labels instead of one! The first label has bold, the second has normal and the third has green text in it. But actually this is not a good choice at all. The best you can do is to use NSAttributeString:

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.

The first step you need to preform is to create a NSMutableAttributedString object. The reason we create a NSMutableAttributedString instead of NSAttributedString is because it enables us to append string to it. Here’s the code:

NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:@"This is a text"];
//Bold the first four characters.
[stringText addAttribute: NSFontAttributeName value: [NSFont fontWithName: @"Helvetica-Bold"] range: NSMakeRange(0, 4)];
// Sets the font color of last four characters to green.
[stringText addAttribute: NSForegroundColorAttributeName value: [UIColor greenColor] range: NSMakeRange(9, 4)];

How it works!

The first line initialize the NSMutableAttributedString object with the value “This is a text”. Then we use use the addAttribute:value:range: method to add attributes to a specific range of characters.
At line three we change the font type of first four characters to bold.
Finally we change the color of last four characters to green.

EPUBs are much better than PDFs!

It’s a fact that we really can’t live and grow without reading books. I remember a poetry from my elementary school that was saying “Book is your best friend.” It’s not my best friend in fact:-D But one of my best friends. EPUB and PDF

Anyway, book is important and we can’t ignore it. As a result, companies are trying to make it better every day. With the advancement of technology, IT companies decided to invest on book industry; consequently, on 15 June 1993, Adobe released the very first version of Acrobat Reader with support PDF 1.0. The goal of Adobe was to enable users to read and share documents in a file rather than a heavy book. PDF format became very popular as it’s the most popular file format for electronic books and documents today.

Everything was OK and PDF was rocking until September 2007! International Digital Publishing Forum (IDPF) released a new format for electronic books called EPUB.
EPUB (short for electronic publication) is a free and open e-book standard. Files have the extension .epub. EPUB is designed for reflowable content, meaning that an EPUB reader can optimize text for a particular display device. EPUB also supports fixed-layout content. The format is intended as a single format that publishers and conversion houses can use in-house, as well as for distribution and sale. It supersedes the Open eBook standard. WOW THAT IS AWESOME!!!

Amazon KindleWith the introduction of iPad, a new way to read books has opened. People prefer to carry an iPad rather than lots of books; also, reading books on a tablet is more enjoyable than a book. IT companies like Apple, Amazon, Samsung, Nook and even Microsoft did as good as they could to find a way to make it even better and easier to read books.

Amazon was the first to use EBUP files in their devices. The first version of Amazon Kindle supports EPUB format. Apple also has support for EPUB as well. In fact, the default format for all iBooks Store books are EPUB; also, the Apple’s new textbooks are EPUB too. Everybody is talking about EPUB because of its awesomeness.

Here are some of advantages of EPUB over PDF:

  • EPUB files are much lighter in size than PDFs.
  • You can customize the font type and size of the book you’re reading.
  • Background and text colors also can be changed to whatever colors you want.
  • Searching through the EPUB books are much faster and easier while the EPUB files contains XML files.

Although EPUB books are amazing but can’t replace the PDFs yet. PDF format still have a lot of advantages over EPUB like encryption, support for filling a form and more. However, IDPF is working on next version of EPUB to have more features.

Find out how your customers interact with your mobile apps with Flurry

It’s about three months I’m developing iOS applications for Pichak. When developing apps that going to have a lot of users and want your apps to be successful, you need to know how users working with it.

Gathering usage data is a pretty easy task in web applications because users must visit your website which is hosted on your server; therefore, you can get almost any kind of data you need. On the contrary, when it comes to mobile apps, gathering such data is a pain in the neck; but, some companies have established handy tools which help you get usage data much easier. There are a lot of services out there you can use but I really recommend you to use Flurry!

Introducing Flurry Analytics

Flurry Analytics is a FREE service you can use to understand how your customers interact with your mobile applications. Flurry has a lot of features (all of them are free to use) and I want to mention some of its important features from in my opinion:

  • It’s Free (Again)!
  • It support all major mobile operating systems like iOS, Android and Windows Phone. It also supports Blackberry, HTML5/Hybrid Apps/MobileWeb and JavaME
  • It supports crash report! So when you app crashes the Flurry service send you the error! So you know exactly when, where and how the error happened.
  • It support Geo-location; so you can see where your users coming from. This feature is very useful. You can use location data to plan for your app’s future and add more languages to it based on these data.
  • It’s very easy to implement. It takes about 5 minutes getting things ready and about 30 seconds to implement it in each part of your application.
  • It works asynchronously so your customers won’t realize it.
  • It supports SSL encryption so you can be sure usage data won’t be stolen.
  • It still sending you data even if the app is in background.
  • Gathered data can be categorized by age, gender and more.

How to get started

The first think you need to do is to sign up for a free Flurry account. Then, you can submit your app and it gives you an API key. In fact the API key is almost all you need. Depend on your app’s platform you can download the SDK and adding it to your application.

I used the iOS version of the SDK and it was supper easy to use. All I needed to do was to add Flurry’s library and Flurry.h header file to my application! Boom! Everything was ready to start!

I hope you use this service and enjoy and I do.

Moving to WordPress!

As you may know, I’m blogging for about 7 years now. My very first post was published on a Microsoft free blogging platform called Windows Live Spaces which discontinued its services and I don’t know why; but, here’s a the answer a Windows Live Spaces user asked them about it:

windowsLiveSpacesDead

Anyway, after Windows Live Spaces, I registered my own domain and started to blog using BlogEngine.NET which is a very successful .NET based blog engine. I used this engine for about 6 years and I was pretty satisfied until their last version which (2.9) which requires .NET Framework 4.5 to be installed on the server. Also, as far as I know, they don’t support Mono anymore. So I decided to move to a more common blog engine which is WordPress of course.

WordPress is open source and can be installed on almost any platform just by installing PHP and My SQL which are both free and open source as well and I think it’s the best choice for you if you’re going to start blogging. WordPress has also offered a free website for those who doesn’t have or doesn’t want to have personal websites and servers so people can go to WordPress.com and start blogging immediately!

Migrating to WordPress from BlogEngine is not easy as it sounds! In fact, moving from a Windows-based blog engine to a Linux-based one is a pain in the neck but when you really decide to migrate you can 😀
I used Dave Burke’s post on .NET to WordPress: Migrating BlogEngine.NET and also a BlogML importer plug-in to move to WordPress. If you want to move as well, checkout Dave’s post!

Besides moving to WordPress, I also migrate my server from Windows to Linux! Linux server are more secure, more stable and more affordable!

It’s about a week I’m working with WordPress and I think it’s awesome. Everything is put in its place. The WordPress editor is outstanding and I think there’s no need to use Windows Live Writer anymore. It’s very simple while powerful! This was one of my biggest problems when was blogging on BlogEngine.NET. BlogEngine’s web interface was not so good so I had to use Windows Live Writer as an alternative and since Windows Live Writer is only available for Windows OS, blogging on other operating systems like Mac was a very difficult task to do.

I highly recommend you to choose WordPress if you want to start blogging or move to a new blogging platform 🙂