Home > front end >  Can i set a Custom UIFont to italic in an UILabel?
Can i set a Custom UIFont to italic in an UILabel?

Time:11-26

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];

**

CodePudding user response:

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

CodePudding user response:

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";
  • Related