Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you bold just one word in a UILabel?

Tags:

iphone

It's easy to display a single label with entirely the same text but what happens when you want to display one word in bold?

Example:

All your bases are belong to us.

like image 905
Gary Haran Avatar asked Oct 22 '10 15:10

Gary Haran


People also ask

How do you make words in bold?

Type the keyboard shortcut: CTRL+B.


1 Answers

UILabel itself cannot draw a label with different attributes/fonts within the text. But Core Text can. You can create a subclass of UILabel that wraps an attributed string and uses Core Text to draw it. You can handle the drawing as in this sample code from the Core Text programming guide:

// Build up your attributed string, then...
CTLineRef line = CTLineCreateWithAttributedString(attrString);

// Set text position and draw the line into the graphics context
CGContextSetTextPosition(context, 10.0, 10.0);
CTLineDraw(line, context);
CFRelease(line);
like image 132
Jeremy W. Sherman Avatar answered Sep 23 '22 00:09

Jeremy W. Sherman