UILabel-不建议使用的方法"adjustsLetterSpacingToFitWidth"的替代方法; [英] UILabel - Alternative for Deprecated Method "adjustsLetterSpacingToFitWidth"

查看:83
本文介绍了UILabel-不建议使用的方法"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;

将其放置在UILabel的实现中.谁能告诉我这的确切替代方法是什么?该文档说-使用NSKernAttributeName,但是我不太了解.有人可以帮我吗?

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;

在我的代码中,我有:

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

- (void) setter:(BOOL) setVal {
     self.adjustsLetterSpacingToFitWidth = setVal;
}
@end

推荐答案

首先,您的gettersetter完全多余,如图所示;无论您在哪里调用getter和/或setter,都可以直接直接获取/设置adjustsLetterSpacingToFitWidth.

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.

关于如何使用NSKernAttributeName进行自动字距调整的问题,Apple的文档说:要打开标签中的自动字距调整,请将字符串的NSKernAttributeName设置为[NSNull null]",即,会做类似的事情:

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.)

这篇关于UILabel-不建议使用的方法"adjustsLetterSpacingToFitWidth"的替代方法;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆