简体   繁体   English

iOS / Objective-C UILabel文本字距调整

[英]iOS/Objective-C UILabel text kerning

I was in search of how to increase character spacing in UILabel s to make more attractive my UI Design implementations for the Apps I do. 我一直在寻找如何增加UILabel的字符间距,以使我所使用的Apps的UI设计实现更具吸引力。 And I found following answer, which tells it allows to adjust the Text Kerning , and it's written in Swift . 我找到了以下答案,它告诉我们它可以调整Text Kerning ,并且它是用Swift编写的。

But what I needed is an Objective-C Solution. 但是我需要的是一个Objective-C解决方案。 So I tried to convert the following code snippet: 因此,我尝试转换以下代码段:

import UIKit
@IBDesignable
class KerningLabel: UILabel {
    @IBInspectable var kerning:CGFloat = 0.0{
        didSet{
            if ((self.attributedText?.length) != nil)
            {
                let attribString = NSMutableAttributedString(attributedString: self.attributedText!)
                attribString.addAttributes([NSKernAttributeName:kerning], range:NSMakeRange(0, self.attributedText!.length))
                self.attributedText = attribString
            }
        }
    }
}

to following in Objective-C: 在Objective-C中遵循以下要求:
KerningLabel.h: KerningLabel.h:

#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface KerningLabel : UILabel
@property (nonatomic) IBInspectable CGFloat kerning;
@end

KerningLabel.m: KerningLabel.m:

#import "KerningLabel.h"

@implementation KerningLabel
@synthesize kerning;
- (void) setAttributedText:(NSAttributedString *)attributedText {
    if ([self.attributedText length] > 0) {
        NSMutableAttributedString *muAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        [muAttrString addAttribute:NSKernAttributeName value:@(self.kerning) range:NSMakeRange(0, [self.attributedText length])];
        self.attributedText = muAttrString;
    }
}
@end

And it gives the following Attribute in XCode IB to adjust the Kerning, 并且它在XCode IB中提供了以下属性来调整字距调整, 在此处输入图片说明

But it really doesn't seems to be taking effect on the UI when the App is running and also in the Interface Builder the text goes disappearing. 但是,当应用程序运行时,它似乎并没有在UI上生效,而且在Interface Builder中,文本也消失了。

Please somebody help me and point out what I have done wrong. 请有人帮助我,指出我做错了什么。

Thanks in Advanced! 提前致谢!

You want to update your attributedText every time kerning is updated. 您希望每次字距更新时都更新您的attributedText So, your .h should look like: 因此,.h应该如下所示:

IB_DESIGNABLE
@interface KerningLabel : UILabel

@property (nonatomic) IBInspectable CGFloat kerning;

@end

and your .m : 和你的.m:

@implementation KerningLabel

- (void)setKerning:(CGFloat)kerning
{
    _kerning = kerning;

    if(self.attributedText)
    {
        NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
        [attribString addAttribute:NSKernAttributeName value:@(kerning) range:NSMakeRange(0, self.attributedText.length)];
        self.attributedText = attribString;
     }
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM