2016-06-07 1 views

Répondre

9

Vous pouvez utiliser l'attribut NSKernAttributeName sur une chaîne attribuée:

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

Merci J2K. J'ai oublié la possibilité d'appliquer le crénage à une sous-chaîne de la chaîne. –

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]; 

Voir aussi ce pour plus d'informations et les résultats: http://www.devsign.co/notes/tracking-and-character-spacing

0

cliquez sur l'étiquette et aller à l'inspecteur des attributs. Changer le texte de plain à attribué. Vous vous aurez plusieurs options pour l'espacement. J'espère que cela t'aides.

+0

pouvez-vous expliquer, parce que je ne vois aucune option pour le "espacement des lettres" –

5

Vous pouvez utiliser un NSAttributedString et jouer avec l'attribut NSKernAttributeName. La valeur par défaut pour ceci est 0 alors vous voudrez le mettre à un nombre négatif.

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

en Obj-C vous pouvez faire quelque chose comme ceci:

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

label.attributedText = attrStr; 

à Swift que vous pourriez faire quelque chose comme ceci:

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

La meilleure façon est de créer un classe UILabel personnalisée et définissez l'espacement des lettres à partir de 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

Le NSKernAttributeName peut être utilisé.

Mais en correction aux autres réponses: Ne pas appliquer à la longueur du texte, mais (text.length - 1).

L'espacement négatif ou positif est ajouté à la lettre et ce n'est pas nécessaire pour le dernier. Supposons que vous ajouteriez un espacement positif qui finirait par un espacement après la dernière lettre. Une chaîne centrée ne semble plus centrée. La même chose s'applique pour l'espacement négatif.

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]; 

appliquée à pleine longueur de texte.

Applied to full text length.

Appliqué à (longueur du texte - 1). Applied to (text length - 1).