2016-06-07 37 views

risposta

9

È possibile utilizzare l'attributo NSKernAttributeName su una stringa attribuito:

UILabel *l = [UILabel new] 

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"127"] 

//the value paramenter defines your spacing amount, and range is the range of characters in your string the spacing will apply to. Here we want it to apply to the whole string so we take it from 0 to text.length. 
[text addAttribute:NSKernAttributeName value:[NSNumber numberWithDouble:-0.5] range:NSMakeRange(0, text.length)]; 
[l setAttributedText:text]; 
+0

Grazie J2K. Ho dimenticato la possibilità di applicare la crenatura a un subrange della stringa. –

1
NSString *myString = @"127"; 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:myString]; 

float letterSpacing = -1.50f; // change spacing here 
[attributedString addAttribute:NSKernAttributeName value:@(letterSpacing) range:NSMakeRange(0, [myString length])]; 
[myLabel setAttributedText:attributedString]; 

vedono anche questo per più informazioni e risultati: http://www.devsign.co/notes/tracking-and-character-spacing

0

clic sull'etichetta e vai al tuo attributi ispettore. Cambia il testo da semplice a attribuito. Vi saranno diverse opzioni per la spaziatura. Spero che questo ti aiuti.

+0

puoi spiegare, perché non vedo alcuna opzione per la "spaziatura delle lettere" –

5

è possibile utilizzare un NSAttributedString e giocare con l'attributo NSKernAttributeName. Il valore predefinito per questo è 0, quindi dovrai impostarlo su un numero negativo.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/#//apple_ref/doc/constant_group/Character_Attributes

in Obj-C si può fare qualcosa di simile:

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc]initWithString: @"Test test test test "]; 
[attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)]; 

label.attributedText = attrStr; 

a Swift si potrebbe fare qualcosa di simile:

let myTitle = "my title" 
let titleLabel = UILabel() 
let attributes: NSDictionary = [ 
    NSFontAttributeName:UIFont(name: "FONT_NAME", size: TEXT_SIZE), 
    NSKernAttributeName:CGFloat(2.0) 
] 

let attributedTitle = NSAttributedString(string: myTitle, attributes:attributes as? [String : AnyObject]) 

titleLabel.attributedText = attributedTitle 
15

Il modo più semplice è quello di creare un classe UILabel personalizzata e imposta la spaziatura delle lettere da Storyboard.

open class CustomLabel : UILabel { 
    @IBInspectable open var characterSpacing:CGFloat = 1 { 
     didSet { 
      let attributedString = NSMutableAttributedString(string: self.text!) 
      attributedString.addAttribute(NSKernAttributeName, value: self.characterSpacing, range: NSRange(location: 0, length: attributedString.length)) 
      self.attributedText = attributedString 
     } 

    } 
} 

enter image description here

2

Il NSKernAttributeName può essere utilizzato.

Ma in correzione alle altre risposte: Non applicare alla lunghezza massima del testo, ma (text.length - 1).

La spaziatura positiva o negativa viene aggiunta alla lettera e non è richiesta per l'ultima. Supponiamo che tu aggiunga una spaziatura positiva che finirebbe in una spaziatura dopo l'ultima lettera. Una stringa centrata non sembra più centrata. Lo stesso vale per la spaziatura negativa.

NSString *text = @"Sample Text";  
UILabel *label = [UILabel new] 

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: text]; 
[attributedString addAttribute:NSKernAttributeName value:[NSNumber numberWithDouble:-1.0] range:NSMakeRange(0, text.length-1)]; 

[label setAttributedTitle:attributedString forState:UIControlStateNormal]; 

Applicato a lunghezza del testo integrale.

Applied to full text length.

Applicato (lunghezza del testo - 1). Applied to (text length - 1).