Change the Font Style - (Set Lable text in All-Caps format ) in Objective C

11.9k Views Asked by At

I want to set the UILable text font style in Small-Caps format like below image.

Please give me the solution for this if anyone know,

enter image description here

Thanks. :)

7

There are 7 best solutions below

3
aslı On

If I didn't get you wrong, is this what you want?

NSString *uppercaseString = [yourString uppercaseString];
1
Hiren On

try with this one

    NSString *string = @"askdfjgjksdfgh";
NSString *upString = [string uppercaseString];
NSLog(@"U %@", upString);
0
Mudit Bajpai On

you can try with this

NSString* str=@"mudit"; label.text=[str uppercaseString];

it will give you the output like this:MUDIT

2
Tomasz Wojtkowiak On

Fonts available in iOS don't have "capitalic style". You should add own font or try to create font using function CTFontDescriptorCreateCopyWithFeature.
I think that the simplest way will be to build attributed string (NSAttributedString) with mixed font sizes.

0
dimme On

It is not possible to change the font size or type of a individual letter within a UILabel.

What you want to do is to have 2 labels, one in the begging for the first bigger letter and one right after that for the remaining word.

In order to access the first letter and the rest of the word you may use:

NSString * word = @"COMPLETED";
NSString * firstLetter = [word substringToIndex:1];
NSString * remainingWord = [word substringFromIndex:1];

What you see in the picture are probably pictures of words and not UILabels.

0
Jasper Blues On

To make the UILabel render as an upper-case string, where the first letter of each word is larger, you can do something like this:

@implementation CapitalicTextLabel

- (void)drawRect:(CGRect)rect
{

    // Drawing code
    NSArray* words = [self.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetCharacterSpacing(context, 1);
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
    CGContextSetTextMatrix (context, myTextTransform);

    CGFloat x = 0;
    float centeredY = (self.font.pointSize + (self.frame.size.height - self.font.pointSize) / 2) - 2;
    CGFloat firstLetterSize = self.font.pointSize * 1.4;

    for (NSString* word in words)
    {
        NSString* letter = [[word substringToIndex:1] uppercaseString];
        CGContextSelectFont(context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], firstLetterSize, kCGEncodingMacRoman);
        CGContextShowTextAtPoint(context, x, centeredY, [letter cStringUsingEncoding:NSASCIIStringEncoding], [letter length]);
        x = CGContextGetTextPosition(context).x;

        NSString* restOfWord = [[[word substringFromIndex:1] uppercaseString] stringByAppendingString:@" "];
        CGContextSelectFont(context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize,
            kCGEncodingMacRoman);
        CGContextShowTextAtPoint(context, x, centeredY, [restOfWord cStringUsingEncoding:NSASCIIStringEncoding], [restOfWord length]);
        CGPoint v = CGContextGetTextPosition(context);
        x = CGContextGetTextPosition(context).x;
    }

}

@end

This implementation doesn't handle splitting across multiple-lines or honor the TextAlignment setting, This would be should be simple enough to add afterwards.

Example:

enter image description here

0
Legoless On

For iOS 7 custom fonts, the method is described by Anthony Mattox. For system font I do not know a way.

Typesetting a font in small caps on iOS