Raj
Raj

Reputation: 1213

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

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. :)

Upvotes: 7

Views: 11909

Answers (7)

Legoless
Legoless

Reputation: 11112

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

Upvotes: 3

Jasper Blues
Jasper Blues

Reputation: 28766

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

Upvotes: 1

dimme
dimme

Reputation: 4424

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.

Upvotes: 0

Tomasz Wojtkowiak
Tomasz Wojtkowiak

Reputation: 4920

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.

Upvotes: 3

Mudit Bajpai
Mudit Bajpai

Reputation: 3020

you can try with this

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

it will give you the output like this:MUDIT

Upvotes: 1

Hiren
Hiren

Reputation: 12780

try with this one

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

Upvotes: 0

aslı
aslı

Reputation: 8914

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

NSString *uppercaseString = [yourString uppercaseString];

Upvotes: 11

Related Questions