iOS / Objective-C UILabel文本字距调整 [英] iOS/Objective-C UILabel text kerning

查看:134
本文介绍了iOS / Objective-C UILabel文本字距调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在 UILabel 中增加字符间距,以便为我的应用程序提供更具吸引力的UI设计实现。我发现以下



但它似乎并没有在UI上生效当应用程序运行时,在界面生成器中文本也会消失。



请有人帮助我并指出我做错了什么。



谢谢高级!

解决方案

您想要更新 attributedText 每次更新字距时。所以,你的.h应该是这样的:

  IB_DESIGNABLE 
@interface KerningLabel:UILabel

@property(非原子)IBInspectable CGFloat kerning;

@end

和你的.m:

  @implementation KerningLabel 

- (void)setKerning:(CGFloat)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


I was in search of how to increase character spacing in UILabels to make more attractive my UI Design implementations for the Apps I do. And I found following answer, which tells it allows to adjust the Text Kerning, and it's written in Swift.

But what I needed is an Objective-C Solution. 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:
KerningLabel.h:

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

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,

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.

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. So, your .h should look like:

IB_DESIGNABLE
@interface KerningLabel : UILabel

@property (nonatomic) IBInspectable CGFloat kerning;

@end

and your .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

这篇关于iOS / Objective-C UILabel文本字距调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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