Can I install a different font in One UIlabel?

I have a line like "This is a good apple." to display on my UIlabel. I would like to set the word "good" with a different font. Just look: "This apple is good .

+6
source share
6 answers

Take a look at NSAttributedString ... Use OHAttributedLabel to draw NSAttributedString, because UILabel, UITextView does not support NSAttributedString ...

PS: if you plan to distribute an iOS6 or newer application, since UILabel now supports NSAttributedString, you should use UILabel directly, not OHAttributedLabel, because it is now supported by the operating system.

+5
source

There is a way to set different / multiple fonts and other properties on a shortcut using NSMutableAttributedString. Foll is my code:

UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0]; NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName]; NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict]; UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0]; NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName]; NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict]; [VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))]; [AattrString appendAttributedString:VattrString]; lblText.attributedText = AattrString; 

Note that lblText is a UILabel, output as the owner of the file.
You can continue to add as many NSMutableAttributedString as he wants.

Also note that in my project, the font verdana and arial are added and a layer is added for this.

+4
source

This is not possible in UILabel. But you can use the body text .

Another easier approach would be to use a very small UIWebView. In this case, you can install the font using the html commands.

+1
source

I'm sorry, but this is not possible in the implementation of UILabel, you have two options:

  • You can create a UIWebView and put HTML text
  • You can make your own implementation of UILabel with this versatility, for example, Body text
+1
source

Instead of using two UILabels, you can have one label with several font styles and colors. Here is an example:

 UILabel *customLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 25)]; customLbl.backgroundColor = [UIColor yellowColor]; [self createTwoTextStyleInSingleUILabel:customLbl];// custom method calling [self.view addSubview:customLbl]; 

The custom method is as follows: Before applying this method, add the QuartzCore framework (required for CALayers) and Framework CoreText (required for the attribute string) to the project.

  #import <QuartzCore/QuartzCore.h> #import <CoreText/CoreText.h> - (void)createTwoTextStyleInSingleUILabel: (UILabel *) myLabel{ NSString *firstText = NSLocalizedString(@"First text:", nil); NSString *secondText = [NSString stringWithFormat:@"%@ %@",firstText,@"Second text"]; CATextLayer *myLabelTextLayer; /* Create the text layer on demand */ if (!myLabelTextLayer) { myLabelTextLayer = [[CATextLayer alloc] init]; myLabelTextLayer.backgroundColor = [UIColor clearColor].CGColor; myLabelTextLayer.wrapped = NO; CALayer *layer = myLabel.layer; //assign layer to your UILabel myLabelTextLayer.frame = CGRectMake((layer.bounds.size.width-180)/2 + 10, (layer.bounds.size.height-30)/2 + 10, 180, 30); myLabelTextLayer.contentsScale = [[UIScreen mainScreen] scale]; myLabelTextLayer.alignmentMode = kCAAlignmentCenter; [layer addSublayer:myLabelTextLayer]; } /* Create the attributes (for the attributed string) */ // customizing first string CGFloat fontSize = 16; UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize]; CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL); CGColorRef cgColor = [UIColor redColor].CGColor; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)ctBoldFont, (id)kCTFontAttributeName, cgColor, (id)kCTForegroundColorAttributeName, nil]; CFRelease(ctBoldFont); // customizing second string UIFont *font = [UIFont systemFontOfSize:16]; CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL); CGColorRef cgSubColor = [UIColor blackColor].CGColor; NSDictionary *subAttributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)ctFont, (id)kCTFontAttributeName,cgSubColor, (id)kCTForegroundColorAttributeName, nil]; CFRelease(ctFont); /* Create the attributed string (text + attributes) */ NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:secondText attributes:attributes]; float lengthOfSecondString = 12.0; // length of second string including blank space inbetween text, space in front , space after text.. Be careful, your app may crash here if length is beyond the second text length (lengthOfSecondString = text length + blank spaces) [attrStr addAttributes:subAttributes range:NSMakeRange(firstText.length, lengthOfSecondString)]; // you can add another subattribute in the similar way as above , if you want change the third textstring style /* Set the attributes string in the text layer :) */ myLabelTextLayer.string = attrStr; myLabelTextLayer.opacity = 1.0; //to remove blurr effect } 
+1
source

Akshay's modified answer and implemented it using Swift:

 // First part with font size 10 let firstPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(10.0)] var attributedString = NSMutableAttributedString(string: "First Part", attributes: firstPartInfo) // Second part with font size 20 let secondPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(20.0)] attributedString.appendAttributedString( NSAttributedString(string: "Second Part", attributes: secondPartInfo)) // Lastly set the attributed text label.attributedText = attributedString 

There is no reason to use third-party solutions or subclasses.

0
source

Source: https://habr.com/ru/post/913705/


All Articles