Keep kerning constant on UILabel's text when justified

Hi,

I'm trying to keep a constant kerning on a UILabel's text when I set it as justified.


I've tried setting it explicitly as an attribute, and it works just fine for all the alignment modes but justified.

let attributes = [NSAttributedString.Key.kern: 1.0]
let attributedString = NSAttributedString(string: label.text!, attributes: attributes)
label.attributedText = attributedString


Any clue to how should I tackle this?
As a test, I'm working on a blank iPhone Xr screen. UILabel centered horizontally and vertically. Set its width to 280, lines to 0 and text to: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

I'd like it to keep a constant kerning while varying the space between words in order to satisfy the alignment mode of justified.

What do you mean by constant kerning ?

If you want no-kerning, set value to 0 (as default).

Kerning of 1 will reduce space by 1 point.

Kerning is a constant, anyway, so do you want variable kerning ?


I tested wityh kern of 1 and -1 and 5.

I do not understand what your problem is (a picture on imgur pointing out the issue would help).


Note: that's more a Cocoa Touch question than a Swift.

>that's more a Cocoa Touch question than a Swift.

Thank you I didn't realize it. I'll take it into account next time I'm having issues on this matter.

> I do not understand what your problem is (a picture on imgur pointing out the issue would help).

I know, but I tried posting screenshots in the past and they are probably still being moderated by Apple. Anyway I'll try again with a further post down below.


The issue is that the kerning is not fixed. It changes when the label's text is justified. The space between characters varies: "l i k e th is".
I only want a variable space between words, not characters, to keep it being justified.
As I said above, I'm going to post a reply to this comment with a couple of screenshots to explain it visually.

You cannot post images on this forum, unfortunately


I tested: changing the justification does not seem to change the kerning, but only the words spacing.

Ok, but are you reffering to a native photo uploader or to linking imgur links?
By the way, above I've tried explaining myslef a little bit better and I've additionally posted a comment with the imgur links, currently being moderated

If you need to post a link here, just write with some space


h ttps://

This is what I'm looking at: imgur com/2y5tJ8h -> As you see the first sentence has a different kerning compared to the other ones.

This is what I'm looking for: imgur com/ABeJpW9 -> A justified text that doesn't vary the kerning between characters.

You are right, there is a very clear difference of space between characters.


The point is that kerning sets a difference from the "natural" kerning, not an absolute value.


I found this article that speaks of tracking parameter, but could not find how to use in Swift. May be you'll be better than me on this.

h ttps://www.rightpoint.com/rplabs/ios-tracking-typography


EDITED


I tried a lot of things, to have different attributes for a word (Lorem, that I repeated in text).

But could not get it.


It really seems that justify and fix spacing are incompatible: justify enlarge the spaces everywhere, then kern may reduce / augment, but found no way to set a fix value.

Thank you Claude, I'll look into it.

Accepted Answer

I've found something more.


Not sure that will answer totally, but that does improve.

Add

        paragraphStyle.hyphenationFactor = 1


Look here for details

https://stackoverflow.com/questions/30300323/justify-text-in-ios-swift-or-objective-c-without-breaking-tracking-kerning-let

Hi Claude,

This is the closest I can get. I'm further looking into it. If I come to an even better way of coping with this issue I'm going to post here.
In the meantime I'm gonna keep the solution you linked.
Thank you.

Thanks for the feedback and good continuation.


Don't forget to close the thread.

Keep kerning constant on UILabel's text when justified
 
 
Q