Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Bold and Italic on the same word

Background: I have been trying to display a sentence with Bold and Italic font as well as normal ones.

Question: How can I display something like this "Hello, my name is Byte". Notice that Byte is both bold and italic, while other words remains normal.

I have tried: I think coreText should be able to do something along the line, I just have not been able to find the correct way to do it. I also used TTTAttributeLabel and cannot make it both bold and italic. I have Three20 loaded, just do not know which or what to use. Webview does not work with my background.


As a reply to Carles Estevadeordal:

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 displayed white background.

like image 218
Byte Avatar asked Apr 09 '12 19:04

Byte


People also ask

How do you bold a word in a text message on iPhone?

Short guide: Open the Settings app and tap Accessibility. Tap Display & Text Size. Tap the toggle switch for Bold Text to turn it on.

How do you make words bold and italic in word?

To make text bold, select and highlight the text first. Then hold down Ctrl (the control key) on the keyboard and press B on the keyboard. To make text italic, select and highlight the text first. Then hold down Ctrl (the control key) on the keyboard and then press the I on the keyboard.


1 Answers

After a good night sleep, I found a way to do it using TTTAtributedlabel. Here is 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 do not have a way to add 2 attributes (ie: bold and italic separately) into the same word/letter. But this does the trick.

like image 90
Byte Avatar answered Oct 01 '22 05:10

Byte