2015-09-28 13 views
10

Có vô số các cài đặt cho NSAttributedParagraphStyle mà tôi có thể nhìn thấy trong giao diện Builder:Bạn điều chỉnh văn bản bằng cách sử dụng Trình tạo giao diện trong Xcode 7 như thế nào?

Nhưng không ai trong số này là cho kerning văn bản. Có cách nào để điều chỉnh văn bản trong Trình xây dựng giao diện Xcode 7 cho văn bản được gán không?

(! Xin đừng trả lời với làm thế nào để làm điều này trong mã - Tôi đã biết làm thế nào để làm điều đó)

+0

Bạn đã bao giờ tìm thấy một cách? Điều đó có vẻ dễ thấy khi vắng mặt. – Dov

+0

@Dov nope ... thực sự! – brandonscript

Trả lời

5

Bạn thực sự có thể làm điều này mà không cần dùng một lớp con thông qua một phần mở rộng .

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

Trong khi điều này sẽ không thực sự hiển thị trong giao diện người xây dựng nó sẽ hiển thị và làm việc khi bạn chạy ứng dụng của bạn.

7

Tạo một lớp con của UILabel gọi nó KerningLabel có nó được bao gồm các đoạn mã sau:

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

Kéo nhãn ra. Thay đổi nó thành lớp con UILabel của bạn. Điều chỉnh kerning như mong muốn. enter image description here

Trong obj-c:

.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

Rõ ràng không phải là một lựa chọn bản địa, nhưng một giải pháp khá trơn! Tốt đẹp. – brandonscript

+0

Tôi đang tìm phiên bản Objective-C của cùng một đoạn mã này! Tôi không biết cách chuyển đổi hình ảnh này thành ObjC –

+0

@KasunRandika Xem [tại đây] (https://gist.github.com/bgayman/6c9d1b705b50782e5d19d25f688f6fd9) – beyowulf

0

Một nỗ lực rút ngắn:

@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 
    } 
    } 
} 
Các vấn đề liên quan