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.)
转载:http://www.acnenomor.com/466529p1/uilabel-alternative-for-deprecated-method-adjustsletterspacingtofitwidth