2016-06-02 23 views
6

Quando si utilizza NSKernAttributeName mette uno spazio alla fine di ogni riga, esiste un modo per risolvere questo problema? È possibile impostare l'attributo per essere nell'intervallo:NSKernAttributeName spazio alla fine della riga in una NSAribributoStringa

NSRange(location: 0, length: self.text!.characters.count-1) 

Ma non voglio impostare questo per ogni riga.

Questo è il codice di prova in un parco giochi che sto usando

//: Playground - noun: a place where people can play 

import UIKit 
import XCPlayground 

var text = "Hello, playground\nhow are you?" 

let paragraphStyle = NSMutableParagraphStyle() 
paragraphStyle.paragraphSpacing = 50 
paragraphStyle.alignment = NSTextAlignment.Left 
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail 

let attributes = [ 
    NSParagraphStyleAttributeName: paragraphStyle 
    , NSKernAttributeName: 20 
] 


let attributedString = NSAttributedString(string: text, attributes: attributes) 

let label = UILabel() 
label.attributedText = attributedString 
label.numberOfLines = 0 
label.textColor = UIColor.greenColor() 
label.backgroundColor = UIColor.orangeColor() 
label.sizeToFit() 
label.center = CGPoint(x: 500, y: 100) 


var text2 = "What's up\nWhere are you?" 
let attributedString2 = NSAttributedString(string: text2, attributes: attributes) 

let label2 = UILabel() 
label2.attributedText = attributedString2 
label2.numberOfLines = 0 
label2.textColor = UIColor.greenColor() 
label2.backgroundColor = UIColor.orangeColor() 
label2.sizeToFit() 
label2.center = CGPoint(x: 500, y: 250) 

var text3 = "Hello" 
let attributedString3 = NSAttributedString(string: text3, attributes: attributes) 

let label3 = UILabel() 
label3.attributedText = attributedString3 
label3.numberOfLines = 0 
label3.textColor = UIColor.greenColor() 
label3.backgroundColor = UIColor.orangeColor() 
label3.sizeToFit() 
label3.center = CGPoint(x: 500, y: 400) 

let holderView = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 500)) 
holderView.backgroundColor = UIColor.lightGrayColor() 
holderView.addSubview(label) 
holderView.addSubview(label2) 
holderView.addSubview(label3) 

XCPlaygroundPage.currentPage.liveView = holderView 

Con il risultato simile a questo:

text with kerning at the end of a line

si possono vedere gli spazi al termine di ciascuno dei le linee.

risposta

0

Questa è in realtà la definizione di come funziona il kerning; regola lo spazio tra il carattere crenato e dove sarà il prossimo personaggio. Il fatto che un personaggio successivo possa essere disegnato o meno è irrilevante.

Standard Attributes

L'attributo spaziatura indica quanto il carattere successivo deve essere spostata dal suo offset come definito da carattere del personaggio corrente predefinito; un kern positivo indica uno spostamento più avanti e un kern negativo indica uno spostamento più vicino al carattere corrente.

Se aiuta, pensa a farlo in un elaboratore di testi. Se la crenatura è attiva e si digita un carattere, dove ti aspetteresti che fosse il punto di inserimento? La risposta attesa sarebbe "sfalsata dal carattere appena digitato per la quantità di kern" poiché questo è ciò che accade nel caso predefinito di kern essendo 0, corretto? Bene, questo è esattamente ciò che sta accadendo qui: se si kern l'ultimo carattere di una stringa, la stringa quindi include l'ultima kern.

Quindi la cosa corretta da fare qui è di avvolgere la tua logica dropLast() in un'estensione e chiamarla un giorno.

0

creare un'estensione

import UIKit 
extension UILabel { 

    @IBInspectable var kerning: Float { 
     get { 
      var range = NSMakeRange(0, (text ?? "").characters.count) 
      guard let kern = attributedText?.attribute(NSKernAttributeName, atIndex: 0, effectiveRange: &range), 
      value = kern as? NSNumber 
      else { 
       return 0 
      } 
      return value.floatValue 
     } 
     set { 
      var attText:NSMutableAttributedString? 

      if let attributedText = attributedText { 
       attText = NSMutableAttributedString(attributedString: attributedText) 
      } else if let text = text { 
       attText = NSMutableAttributedString(string: text) 
      } else { 
       attText = NSMutableAttributedString(string: "") 
     } 

      let range = NSMakeRange(0, attText!.length) 
      attText!.addAttribute(NSKernAttributeName, value: NSNumber(float: newValue), range: range) 
      self.attributedText = attText 
     } 
    } 
} 

Questo è stato risposto here

+1

Ciò non sembra risolvere il problema: http://i.stack.imgur.com/7Pwwv.png – richy