7

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

Raj
  • 1,213
  • 2
  • 16
  • 34
  • I want font style like caption "Date Completed" ,"Time Taken" and "Score" which are shown in Image. – Raj Jan 16 '12 at 09:47
  • add font files for that in your xcode – Leena Jan 16 '12 at 09:57
  • The font is Helvetica Neue Bold and i can get font perfectly but problem is that i cant get the same style - means problem is i cant set text style as small-caps :( – Raj Jan 16 '12 at 10:16

7 Answers7

11

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

NSString *uppercaseString = [yourString uppercaseString];
aslı
  • 8,740
  • 10
  • 59
  • 80
  • Then search the font sites for this. If you're going to submit this to apple store then you also need to include the licence of the font, don't forget that. – aslı Jan 16 '12 at 09:54
  • It's not a different font. It's halvtica-bold but just it's style is changed in Photo-shop as All-Caps format, I just want to know that its possible to change this kind of font style in objective-C . :) – Raj Jan 16 '12 at 09:58
  • Well as far as I know, it's not possible to do that in code. You'd better try some workarounds as stated in some of the answers below. – aslı Jan 16 '12 at 10:18
3

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

Community
  • 1
  • 1
Legoless
  • 10,942
  • 7
  • 48
  • 68
3

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.

Tomasz Wojtkowiak
  • 4,910
  • 1
  • 28
  • 35
  • Actually, that is incorrect, at least for iOS 6. The fonts included with iOS are the same variants that come with OS X, and a lot of them include many OpenType features, such as small-caps. – Léo Natan Apr 19 '13 at 15:50
  • Sure. Open the following page in Safari on iOS: http://www.w3schools.com/cssref/playit.asp?filename=playcss_font-variant&preval=small-caps iOS is capable of the same typographic features as it's big brother OS X; it is however much more difficult using these features with the public API. – Léo Natan Jun 06 '13 at 21:27
1

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

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
1

you can try with this

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

it will give you the output like this:MUDIT

Mudit Bajpai
  • 3,010
  • 19
  • 34
0

try with this one

    NSString *string = @"askdfjgjksdfgh";
NSString *upString = [string uppercaseString];
NSLog(@"U %@", upString);
Hiren
  • 12,720
  • 7
  • 52
  • 72
0

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.

dimme
  • 4,393
  • 4
  • 31
  • 51