2015-09-28 12 views
10

私はInterface Builderで見ることができますNSAttributedParagraphStyleの設定の無数があります。Xcode 7のInterface Builderを使用してテキストカーニングを調整するにはどうすればよいですか?

しかし、これらのどれもが、テキストカーニングのためのものです。 Xcode 7のInterface Builderでテキストのカーニングを調整する方法はありますか?

(!コードでこれを行う方法で応答しないでください - 私はすでにそれを行う方法を知っている)

+0

あなたは今までの道を見つけますか?それはその不在の中で目立つように見える。 – Dov

+0

@Dov nope ...確かに! – brandonscript

答えて

5

あなたが実際に拡張を介してサブクラスを使用せずにこれを行うことができます。

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 
     } 
    } 
} 

enter image description here

これは実際にインタフェースビルダーに表示されませんが、それが表示されます、あなたはあなたのアプリケーションを実行するときに働きます。

7

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 
     } 
    } 
} 

ラベルをドラッグします。あなたのUILabelサブクラスに変更してください。必要に応じてカーニングを調整します。 OBJ-Cでenter image description here

.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

+0

明らかにネイティブオプションではありませんが、かなり滑らかなソリューションです!ニース。 – brandonscript

+0

私は同じコードのObjective-Cバージョンを探しています!私はこれをObjCに変換する方法を知らない –

+0

@KasunRandika [ここ](https://gist.github.com/bgayman/6c9d1b705b50782e5d19d25f688f6fd9) – beyowulf

0

短縮の試み:

@IBDesignable class KerningLabel: UILabel { 


    @IBInspectable var kerning: CGFloat = 0.0 { 
    didSet { 
     let attrStr = NSMutableAttributedString(string: "Foobar") 
     attrStr.addAttributes([NSKernAttributeName: kerning], 
          range: NSMakeRange(0, attrStr.string.characters.count)) 
     attributedText = attrStr 
    } 
    } 
} 
関連する問題