Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel - Alternative for Deprecated Method "adjustsLetterSpacingToFitWidth"

So in my code, I was checking if my characters fit in my Label or not and had the following line :

return self.adjustsLetterSpacingToFitWidth;

This was placed in an implementation of UILabel. Can someone tell me just what is the exact alternative for this? The documentation says - Use NSKernAttributeName, but I wasn't quite able to understand that. Can someone help me on this?

In the larger sense - The method is called as:

xLab.adjustLetterSpacingToFitWidth = YES;

In my code, I have:

@implementation UILabel ()
- (BOOL) getter {
    return self.adjustsLetterSpacingToFitWidth;
}

- (void) setter:(BOOL) setVal {
     self.adjustsLetterSpacingToFitWidth = setVal;
}
@end
like image 660
gran_profaci Avatar asked Feb 21 '14 02:02

gran_profaci


1 Answers

First of all, your getter and setter are entirely superfluous as shown; wherever you call getter and/or setter, you could simply get/set adjustsLetterSpacingToFitWidth directly.

As for the question of how to do auto-kerning with NSKernAttributeName, Apple's documentation says: “To turn on auto-kerning in the label, set NSKernAttributeName of the string to [NSNull null]”, i.e., you would do something like:

NSMutableAttributedString *s;
s = [[NSMutableAttributedString alloc] initWithString:my_uilabel.text];
[s addAttribute:NSKernAttributeName
          value:[NSNull null]
          range:NSMakeRange(0, s.length)];
my_uilabel.attributedText = s;

But if you did not want to do automatic adjustment of letter spacing but rather find out whether the text fits in the label or not, you might want to check the various methods in NSString UIKit additions. (This guess of intent is based on the wording in the original question.)

like image 57
Arkku Avatar answered Oct 13 '22 00:10

Arkku