Reputation: 1213
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,
Thanks. :)
Upvotes: 7
Views: 11909
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
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:
Upvotes: 1
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 UILabel
s.
Upvotes: 0
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
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
Reputation: 12780
try with this one
NSString *string = @"askdfjgjksdfgh";
NSString *upString = [string uppercaseString];
NSLog(@"U %@", upString);
Upvotes: 0
Reputation: 8914
If I didn't get you wrong, is this what you want?
NSString *uppercaseString = [yourString uppercaseString];
Upvotes: 11