bra*_*ipt 11 xcode interface-builder kerning ios
NSAttributedParagraphStyle我在Interface Builder中可以看到无数的设置:

但这些都不是用于文本字距调整.有没有办法在Xcode 7的Interface Builder中调整文本字距以获取属性文本?
(请不要回答如何在代码中执行此操作 - 我已经知道如何执行此操作!)
bey*_*ulf 10
创建一个UILabel的子类,调用它KerningLabel让它由以下代码组成:
import UIKit
@IBDesignable
class KerningLabel: UILabel {
@IBInspectable var kerning: CGFloat = 0.0 {
didSet {
if attributedText?.length == nil { return }
let attrStr = NSMutableAttributedString(attributedString: attributedText!)
let range = NSMakeRange(0, attributedText!.length)
attrStr.addAttributes([NSAttributedStringKey.kern: kerning], range: range)
attributedText = attrStr
}
}
}
Run Code Online (Sandbox Code Playgroud)
拖出一个标签.将其更改为您的UILabel子类.根据需要调整字距.
在obj-c:
.h
IB_DESIGNABLE
@interface KerningLabel : UILabel
@property (nonatomic) IBInspectable CGFloat kerning;
@end
Run Code Online (Sandbox Code Playgroud)
.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;
}
}
Run Code Online (Sandbox Code Playgroud)
@结束
小智 7
实际上,您可以在不使用扩展子类的情况下执行此操作.
import UIKit
@IBDesignable
extension UILabel {
@IBInspectable
public var kerning:CGFloat {
set{
if let currentAttibutedText = self.attributedText {
let attribString = NSMutableAttributedString(attributedString: currentAttibutedText)
attribString.addAttributes([NSKernAttributeName:newValue], range:NSMakeRange(0, currentAttibutedText.length))
self.attributedText = attribString
}
} get {
var kerning:CGFloat = 0
if let attributedText = self.attributedText {
attributedText.enumerateAttribute(NSKernAttributeName,
in: NSMakeRange(0, attributedText.length),
options: .init(rawValue: 0)) { (value, range, stop) in
kerning = value as? CGFloat ?? 0
}
}
return kerning
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这实际上不会显示在界面构建器中,但它会在您运行应用程序时显示并运行.
| 归档时间: |
|
| 查看次数: |
2823 次 |
| 最近记录: |