iOS/Objective-C UILabel 文本字距调整
iOS/Objective-C UILabel text kerning
我正在寻找如何增加 UILabel
中的字符间距以使我的 UI 应用程序设计实现更具吸引力。我发现下面的 ,它写在 Swift.
但我需要的是 Objective-C 解决方案。所以我尝试转换以下代码片段:
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
}
}
}
}
关注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
并且它在XCode IB中给出了以下属性来调整字距,
但当应用程序 运行 时,它似乎并没有在 UI 上生效,而且在 Interface Builder 中文本消失了。
请有人帮助我并指出我做错了什么。
感谢进阶!
您想在每次更新字距调整时更新您的 attributedText
。所以,你的 .h 应该是这样的:
IB_DESIGNABLE
@interface KerningLabel : UILabel
@property (nonatomic) IBInspectable CGFloat kerning;
@end
和你的 .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
我正在寻找如何增加 UILabel
中的字符间距以使我的 UI 应用程序设计实现更具吸引力。我发现下面的
但我需要的是 Objective-C 解决方案。所以我尝试转换以下代码片段:
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
}
}
}
}
关注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
并且它在XCode IB中给出了以下属性来调整字距,
但当应用程序 运行 时,它似乎并没有在 UI 上生效,而且在 Interface Builder 中文本消失了。
请有人帮助我并指出我做错了什么。
感谢进阶!
您想在每次更新字距调整时更新您的 attributedText
。所以,你的 .h 应该是这样的:
IB_DESIGNABLE
@interface KerningLabel : UILabel
@property (nonatomic) IBInspectable CGFloat kerning;
@end
和你的 .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