Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change an UILabel/UIFont's letter spacing?

I've searched loads already and couldn't find an answer.

I have a normal UILabel, defined this way:

    UILabel *totalColors = [[[UILabel alloc] initWithFrame:CGRectMake(5, 7, 120, 69)] autorelease];     totalColors.text = [NSString stringWithFormat:@"%d", total];     totalColors.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];     totalColors.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];     totalColors.backgroundColor = [UIColor clearColor];     [self addSubview:totalColors]; 

And I wanted the horizontal spacing between letters, to be tighter, whilst mantaining the font size.

Is there a way to do this? It should be a pretty basic thing to do.

Cheers guys, Andre

UPDATE:

So I was forced to do it like this:

- (void) drawRect:(CGRect)rect  {     CGContextRef context = UIGraphicsGetCurrentContext();      CGContextSelectFont (context, "Arial-BoldMT", 60, kCGEncodingMacRoman);     CGContextSetCharacterSpacing (context, -10);     CGContextSetTextDrawingMode (context, kCGTextFill);      CGContextSetRGBFillColor(context, 221/255.0, 221/255.0, 221/255.0, 221/255.0);     CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);     CGContextSetTextMatrix(context, xform);      char* result = malloc(17);     sprintf(result, "%d", totalNumber);      CGContextShowTextAtPoint (context, 0, 54, result, strlen(result)); } 

But I need to align this to the right. I could do that manually if I knew the width of the drawn text, but it's proving near impossible to find that.

I've read about ATSU, but I couldn't find any examples.

This sucks :/

like image 991
Andre Avatar asked Mar 30 '10 12:03

Andre


People also ask

Can you adjust letter spacing?

Select the text that you want to change. On the Home tab, click the Font Dialog Box Launcher, and then click the Advanced tab. Note: If you're using Word 2007 the tab is called Character Spacing. In the Spacing box, click Expanded or Condensed, and then specify how much space you want in the By box.

How do I change line spacing in UILabel?

"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels." This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.

What is the spacing between letter called?

What is kerning? Kerning is the spacing between individual letters or characters. Unlike tracking, which adjusts the amount of space between the letters of an entire word in equal increments, kerning is focused on how type looks — creating readable text that's visually pleasing.


2 Answers

From iOS 6 you can use NSAttributedString in UILabel.

In attributed string you can use attribute NSKernAttributeName to set letter spacing

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: @"Test test test test "]; [attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)];  UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 300, 100)]; label.attributedText = attrStr; 
like image 133
Krizai Avatar answered Sep 21 '22 21:09

Krizai


I've extended UILabel to change the character spacing. This should work out the box and pulls font, text, color etc from the UILabel itself (proper coding!).

You may notice I draw the text twice, first with clear color. This is to auto center the text in the label. Whilst this may be inefficient - isn't it nice to be auto centered?

Enjoy!

@interface RALabel : UILabel { } @end    @implementation RALabel   - (void) drawRect:(CGRect)rect  {      // Drawing code      CGContextRef context = UIGraphicsGetCurrentContext();     CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman);     CGContextSetCharacterSpacing(context, 1);     CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);     CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );     CGContextSetTextMatrix (context, myTextTransform);      // draw 1 but invisbly to get the string length.     CGPoint p =CGContextGetTextPosition(context);     float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2;     CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);     CGPoint v =CGContextGetTextPosition(context);      // calculate width and draw second one.     float width = v.x - p.x;     float centeredX =(self.frame.size.width- width)/2;     CGContextSetFillColorWithColor(context, [self.textColor CGColor]);     CGContextShowTextAtPoint(context, centeredX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);  } 
like image 34
user363349 Avatar answered Sep 18 '22 21:09

user363349