IOS: bold and italics on the same word

Background . I am trying to display a sentence in bold and italics, as well as regular ones.

Question How can I display something like this "Hello, my name is Byte ." Note that the byte is in bold and italics, while other words remain normal.

I tried : I think coreText should be able to do something along this line, I just could not find the right way to do this. I also used TTTAttributeLabel and cannot make it bold and italic. I have Three20 loaded, I just donโ€™t know what and what to use. Webview does not work with my background.


In response to a question from Karl Estavadord :

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 360, 300, 40)]; [webView setBackgroundColor: [UIColor clearColor]]; [webView loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil]; [self.view addSubview:webView]; 

This is exactly the code I used. It displays a white background.

+6
source share
5 answers

After a good night's sleep, I found a way to do this using TTTAtributedlabel . Here's how:

 TTTAttributedLabel *attLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(x, y, xx, yy)]; NSString *text = @"Hello, my name is Byte"; [attLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^(NSMutableAttributedString *mutableAttributedString) { //font helvetica with bold and italic UIFont *boldSystemFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:10]; CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"Byte" options:NSCaseInsensitiveSearch]; [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange]; CFRelease(font); return mutableAttributedString; }]; 

I still have no way to add 2 attributes (i.e.: bold and italic separately) to the same word / letter. But that does the trick.

+7
source

If you are still interested in a CoreText solution for this problem:

 newFont = CTFontCreateCopyWithSymbolicTraits(fontRef, self.textSize, NULL, kCTFontBoldTrait | kCTFontItalicTrait, kCTFontBoldTrait | kCTFontItalicTrait); [attrString addAttribute:(NSString*)kCTFontAttributeName value:(id)newFont range:[copyString rangeOfString:customText.text]]; 
+2
source

You can use this when the sender is an instance of UIButton

  sender.titleLabel.font=[UIFont fontWithName:@"Helvetica-BoldOblique" size:18.0f]; 
+2
source

To add two attributes (this is not an ARC code):

 #define kLabelFontSize 16.0 NSString labelString = @"Let slide loudly"; [self.tttLabel setText:labelString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) { UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:kLabelFontSize]; CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); if (boldFont) { [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:NSMakeRange(6, 11)]; CFRelease(boldFont); } UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:kLabelFontSize]; CTFontRef italicFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, italicSystemFont.pointSize, NULL); if (italicFont) { [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)italicFont range:NSMakeRange(12, 18)]; CFRelease(italicFont); } }]; return mutableAttributedString; }]; 
+1
source

If the proposal is in a fixed format, the easiest solution is to use 2 different UILabels, one with a normal font and the other with a bold and italic font.

If the sentence is dynamic, you should use the UIWebView object since it was a lavel, and load a simple web page with the text, b> <i> </i> / b>, set in place when you want it to be bold and italic:

 [myWebViewLabel loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil]; 

Hope this helps.

0
source

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


All Articles