I am Nguele

My name is Jean-Dominique Nguele and this is my blog. FLVCTVAT NEC MERGITVR

Tag Archives: swift


Swift: One language to rule them all ?

Reading Time: 3 minutes
Swift, a language to rule them all ?

Swift, a language to rule them all ?

Two years ago at the WWDC, Apple introduced to the world a new programming language: Swift. Bringing to life a new programming language is always a challenge because even if it is the most beautiful syntactically, the lack of use cases may make it only a nice-to-have in some developers mind.

Apple announced to be the future of iOS and OS X apps, which is quite bold since not all developers may learn especially with the expansion of the cross-platform app development using the likes of Xamarin or Titanium.

However, bringing an open-source language allows the community to port it to multiple platforms allowing the benefits of Java without the need of a JVM to run it. I even found out that you can now do scripting using Swift which opens a lots of doors even in terms of DevOps.

However what drew my attention to Swift at first is that summit that occurred a few months ago between Google, Facebook and Uber in London were discussing the language with the possibility of it getting a grown interest in each party. Add another powerhouse like IBM massively supporting and you get what smells like an IETF meeting for a new norm.

Facebook and Uber are both companies trying to use cutting-edge technologies to get the best services possible for their users and are known to use variety of platforms/languages. About Google I can not really say as I do not have a direct input about it, however based on what I read in “How Google test software” it seems they are in a similar position.

Then comes the big IF, what if Google was to drop Java for Android development in favour of Swift ? It might seem unlikely considering the massive following and use of Android in the developers community along with the fact that Java not only is one of the most used languages in the world but also learn in pretty much any computer science related-school.

However Google may have £8.8 billion reasons to make the big jump, those sum up in one word: Oracle. Oracles, always bringers of bad news in Antic Greece but here it is not about them. It is about the Oracle that acquired Sun Microsystems in January 2010, the same Oracle which is now suing Google only to cash in on their investment. Some even speculate that Oracle purchased Sun only so they can sue Google on the conditions of use of Java, even thought I have not made myself an opinion on the matter it is quite intriguing that the beginning of “Oracle VS Google” case was only 7 months after the acquisition.

With this lawsuit hanging over Google’s Android SDK, Swift is more than a valuable option. Along with the fact it is clearly Open-source, it can draw the iOS developers community to port their apps to Android without the heavy refactoring nor the code converters that are not always on point and may not follow the latest updates of this new language that is now on the verge of publishing its third version in two years.

If Google was to make Swift slowly replace Java as the “First class” language for Android development that would not be before a couple of years. But at least if you read it here first and that it turns out to be true, you can still say that you saw it here first and talk about that oracle guy able to predict the future tech trends.

Now imagine a world where you can build Android, iOS and OS X apps with only one language. Add the fact that this language can be compiled on UNIX systems and now even on Microsoft systems thanks to initiatives like Silver for Windows development using the .NET and Windows Phone APIs or Perfect allowing to build easily RESTful server applications. The possibilities are limitless, desktop clients, servers, scripts. You can do so much, so if you were looking to learn a new language, you know what to pick next: Swift.

Thank you for reading, here is a Taylor Swift video

Set tint color on an image in a NSAttributedString

Reading Time: 4 minutes

Introduction

Hi everyone, I have been working, for a few days now, on a project that requires to make an app fully customisable from a configuration file. I was coding and coding and coding, extracting color definitions, applying tints on images, when I ran into an issue. I could not apply tint over a mutable attributed string, nor simple attributed string for that matter. So I was there, looking at my NSAttributedString and my NSTextAttachment without any property allowing me to change only the image color.

It turns out that you actually can apply a color on your attributed string, I don’t know about the underlying magic when using attributed but it seems that you can apply your text color on an image embedded via your text attachment.

Let’s get started

Setting up the UI

First you create your single view project.

Create single view application

Create single view application

Set project name

Set project name

Then you go on your main storyboard (Main.storyboard) and add a label, that you will link to your view controller code (here as labelWithColoredImage).

Label setup

Label setup

Here is what your code should look like:

And that’s it terms of UI setup for now. Now you can run your app to be sure it’s behaving properly and that the label appears.

Simulator capture of a single label

Now the basic NSAttributedString implementation

Now let’s get our hands dirty with the coding part. First let’s update the text and change its color only using the textColor property only. Let’s add the following lines in the viewDidLoad function and run the app again.

First attempt with red color with no magic of NSAttributedString

First attempt with red color

Then we can observe that the text is red. One minor issue is that it doesn’t fit the screen so we can add contraints to the label to center it and make it responsive. Let’s add the text attachment magic now. It’s time to add an image to the project, I just added the image “swift_logo.png” in the project, drag-n-drop works as well as right clicking in the project and selecting “Add files to <ProjectName>”.

Image added to the project

Image added to the project

Let’s create our attributed string now with the text attachment, I will have the text over two lines to show how the image is now part of it and due to the image color I change the text color to blue so you can see the difference.

Another run!

Blue text, original image, thank you NSAttributedString

Blue text, original image

There you have it! Your image integrated within text. Now let’s check what happens if you render your image as a template. You need to replace this line  let image:UIImage=UIImage(named:"swift_logo")!  with  let image: UIImage = UIImage(named: "swift_logo")!.imageWithRenderingMode(.AlwaysTemplate) then run the app again.

Everything is blue now

Everything is blue now

Giving the image a color of its own different from the source one

Now the image is the same color as the text, for some of you it could be enough but we can go a little bit further by giving specifically to the image a color of its own. You can add the following lines before the assignment of the NSAttributedString to labelWithColoredImage.

Basically these lines say to color in green the character from the index matching the character after stringToDisplay for as long as the text attachment length. Now let’s run the app again.

The final step with NSAttributedString

The final step

And there you are, you know know how to apply a different color to an image through a NSAttributedString.

Conclusion

The source code is available on Github

There is one thing to keep in mind though, there must always be a space before the text attachment containing the image in you NSAttributedString otherwise it will not work. I am not quite sure wether the need to add a space is a bug or if it is actually given access to a hidden feature. Either way, happy coding !