0

I have to use SpaceGrotesk google font but does not have an italic style. This is the font https://fonts.google.com/specimen/Space+Grotesk . So my question is can i add it to an UILabel and change it to italic? Any help appreciated. Currently i am using it like this...

   UIFont* nameLabelFont = [UIFont fontWithName:@"SpaceGrotesk-Bold" size:20];
    self.nameLabel.font = nameLabelFont;

UPDATE

**

I used the following command from 2D graphics API and it actually works. 


   CGAffineTransform transfromFontToItalic = CGAffineTransformMake(1, 0, tanf(15 * (CGFloat)M_PI / 180), 1, 0, 0);
 UIFontDescriptor * fontItalic = [UIFontDescriptor fontDescriptorWithName:@"SpaceGrotesk-Regular" matrix:transformFontToItalic]; 
self.usernameLabel.font = [UIFont fontWithDescriptor:fontD size:10];

**

stefanosn
  • 3,264
  • 10
  • 53
  • 79
  • 1
    You can't really. Maybe by redrawing each character with a low level code, but there is no possibility currently. No "fake italic" option. – Larme Nov 25 '21 at 12:27
  • Really not even with fontWithDescriptor? UIFontDescriptor * fontD = [usernameLabelFont.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic]; self.usernameLabel.font = [UIFont fontWithDescriptor:fontD size:5]; – stefanosn Nov 25 '21 at 12:36
  • With font descriptor, it will try to fetch one corresponding to the descriptors, but won't create a new font... Or at least that what's it should be doing in theory. Did you try to see? – Larme Nov 25 '21 at 12:38
  • oh ok i thought it can do it. Because in android it works like this. Yeah i tried actually does not work! – stefanosn Nov 25 '21 at 12:39
  • Open your font file with Font Book to see if it has the Italic typeface. If it doesn't, you have no question to start with. – El Tomato Nov 25 '21 at 12:58
  • You can do it using 2D graphics command CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(15 * (CGFloat)M_PI / 180), 1, 0, 0); UIFontDescriptor * fontD = [UIFontDescriptor fontDescriptorWithName:@"SpaceGrotesk-Regular" matrix:matrix]; self.usernameLabel.font = [UIFont fontWithDescriptor:fontD size:10]; – stefanosn Nov 25 '21 at 13:05
  • DIdn't know about the matrix. Interesting information. If this really works, might want to answer your own question. But, it's indeed a "fake italic", and effect added manually (ans the angle could be anything). – Larme Nov 25 '21 at 13:55
  • Yeah it works correctly but its still fake, but looks like its not! – stefanosn Nov 25 '21 at 13:57

2 Answers2

0

I don't think that it's worth it, but here are the steps:

  1. Convert the label into an image. See How to convert a UIView to an image

  2. Crop the image to about 20 separate images, each one of them representing a separate horizontal line (height: 1, width: the original label width). See Cropping an UIImage

  3. Using code, place the separate images one below the other, with the trailing constraint decremented for each next line

Note: This solution assumes that the label is 1 line

Arik Segal
  • 2,963
  • 2
  • 17
  • 29
0

We can achieve the effect with help of UIFont's class method -

+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)pointSize

and UIKit's UIFontDescriptor method as mentioned below

+ (UIFontDescriptor *)fontDescriptorWithName:(NSString *)fontName matrix:(CGAffineTransform)matrix

In the above FontDescriptor method change the value of C 0 < C < 1 to have the inclination (check CGAffineTransform syntax below)

NB - Syntax of CGAffineTransform is like this - CGAffineTransformMake(CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty)

Code:

CGAffineTransform transform = CGAffineTransformMake(1, 0, 0.5, 1, 0, 0);
UIFontDescriptor * fontDesc = [UIFontDescriptor fontDescriptorWithName:@"SpaceGrotesk-Regular" matrix:transform];
self.lblUsername.font = [UIFont fontWithDescriptor:fontDesc size:20];
self.lblUsername.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
self.lblUsername.text = @"A quick brown fox, jumps over the lazy dog";
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43