Centre a one line text in a UITextView

I had a problem. First I have set up an UITextView with these parameters:

view.backgroundColor = UIColor.blueColor()
view.text = "An example text"
view.textColor = UIColor.whiteColor()
view.textContainerInset.top = 0
view.textContainerInset.bottom = 0

I wanted to be able to centre the text in the view vertically. This should be easy, one just needs to position the top of the text according to the formula: top = (container.height - content.height) / 2. However, when you look at the actual look of the UITextView you will see this:

view.font = UIFont.boldSystemFontOfSize(100.0)

view.font = UIFont(name: "DINCondensed-Bold", size: 100.0)

Ouch, there is a semi-randomly placed offset at the top of the text. Turns out the point size of a font is not always the one you have demanded. However it is possible to get the actual height. An UIFont class provides a bunch of useful metrics:

view.font = UIFont.boldSystemFontOfSize(100.0)
view.font.capHeight // 70.5
view.font.pointSize // 100.0
view.font.ascender // 95.2
view.font.lineHeight // 119.3

view.font = UIFont(name: "DINCondensed-Bold", size: 100.0)
view.font.capHeight // 71.2
view.font.pointSize // 100.0
view.font.ascender // 71.2
view.font.lineHeight // 100.0

On a graph this would look like this:

Finally, the method you can now use is:

extension UITextView {
  func centerTextVertically() {
    guard let ascender = view.font?.ascender else {
      return
    }
    guard let capHeight = view.font?.capHeight else {
      return
    }
    view.textContainerInset.top = (view.bounds.height - ascender - (ascender - capHeight)) / CGFloat(2.0)
  }
}

Note that this will only for one liners and only if you know the height of the UITextView (i.e.: the height was given to it on creation)

The one argument for keeping the 3.5mm jack

The rumour about Apple removing the 3.5mm jack in the future iPhone has wreaked havoc around the Internet. There are several arguments for and against it, but I think there is only one that is valid: it would make it impossible to create accessories for iPhone without going through the MFI process.

Case in point, Apple has recently featured this application in their advert “Ridiculously Powerful”.

1

This application would probably never exist without the possibility to leverage a cheap and universal interface with the device.

Pondering the new iPhone lineup

Here is a theory. There have been quite a lot of rumours about a new “iPhone 6C” which ought to have the 6’s internals in a brand new 4" plastic body. However, with no parts leaks this late into summer, the existence of this model is not a given. (side note: there are no leaks for the iPhone 6S plus out either)

We know that Apple has already broken the pattern of older devices moving down the price range with the 5C. I think this year the pattern could be broken again.

After the iPhone 5C was introduced, the Internet was swamped with articles describing how much of a flop it was. The reality is that most people who buy iPhones in October buy the latest and beefiest one.

So, what if Apple introduced the new, plastic, iPhone 6C in September but only made it available in shops in December for the holidays? This would give them approximately until October to ramp up the production, which would explain the lack of the leaks.

Not only would they protect themselves. But they would also get an excuse for the lower number of phones sold.


Edit 2015-09-14

Well, first predition, first failure. My score so far: 0 out of 1.

Gulf is now on GitHub

In order to clean up all stuff I am moving the Gulf theme to a GitHub repository. Currently it is available for QtCreator and Xcode. A Vim port will follow soon-ish.

I’ve decided to simplify the website’s source. I went from 20 SCSS files to one. My goal is to serve no cruft. I’ve gone through all superfluous classes (bye bye clearfix). The grid is gone in favour of flex.

I’ve added my GitHub page to social links and changed the icons to SVG.

Next step is moving Gulf to GitHub, getting rid of jQuery and finally; writing.

I wonder if the people who complain about the Monument Valley in app purchase also think that paying $30 for a Starcraft 2 expansion is preposterous. After all, they’ve paid 60 bucks already, no?

Maybe sequels to movies should be free too while we are at it.

More gratuitous hate

This has been in Rennes for some while now. One has to wonder what this particular person’s feud with Google is to express their hate like this.

In any case, it is quite an impressive feat.

People will go to great heights to diss someone they dislike

Moving to static website

So I have jumped on the bandwagon and decided to generate my website statically1. For you, it basically means that the site will be a lot faster. For me, I can finally write stuff in markdown only and not worry that some web-editor will butcher it.

Also, the search field has disappeared, but I am looking for a different option. Either Google custom search or Bing. I will see.

Ciao


  1. I am using the Middleman with a modified Bones theme ↩︎