Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I needed to do this with a UILabel, I created a subclass of UIView and implemented custom drawing myself. It's very detailed, but it's not that complex. I ended up passing in HTML-like syntax that handled for bold (&lt; b > and &lt; / b >) and new line (&lt; br >) formatting. I think you could likely do the same for underline using (&lt; u > and &lt; / u >). </p> <p>Basically, my technique was to split the string using the HTML-like tokens (spitting first on the "br" tags, then on "b" tags) and then split each substring into "words", and then handle measuring and drawing each word on its own. If a word didn't fit on the current line, I'd wrap to the next line. It's not perfect, and it doesn't handle for a LOT of scenarios (for instance, a new line tag cannot be placed inside a bold tag), but it works for my intentions. </p> <p>Unfortunately, I don't know why text formatting isn't better supported in some (most/all?) of the iOS basic text display controls. </p> <hr> <p><em><strong>Edit/Update:</em></strong><br> I'll go ahead and add the UIView subclass (replacement for UILabel) I wrote. Use at your own risk! :-)</p> <p><code>MMFormattedTextView.h</code></p> <pre><code>#import &lt;UIKit/UIKit.h&gt;&lt;br&gt; @interface MMFormattedTextView : UIView { int InsetLeft; int InsetTop; NSString *LabelText; UIFont *LabelFont; } @property (assign, nonatomic) int InsetLeft; @property (assign, nonatomic) int InsetTop; @property (strong, nonatomic) NSString *LabelText; @property (strong, nonatomic) UIFont *LabelFont; - (NSInteger)numberOfLinesForRect:(CGRect)rect; @end </code></pre> <p><code>MMFormattedTextView.m</code></p> <pre><code>#import "MMFormattedTextView.h" @implementation MMFormattedTextView @synthesize InsetLeft; @synthesize InsetTop; @synthesize LabelFont; @synthesize LabelText; // LIMITATION: Each bolded section must reside IN BETWEEN &lt;br&gt; tags; it MAY NOT span &lt;br&gt; tags!!!! - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextClearRect(ctx, rect); // Sets up the first position, which is 1 line "off the top", // adjusted so that the text will be centered when it's all drawn CGFloat howManyLinesWouldFit = rect.size.height / [[self LabelFont] lineHeight]; NSInteger howManyLinesDoWeHave = [self numberOfLinesForRect:rect]; CGFloat lineOffset = (howManyLinesWouldFit - howManyLinesDoWeHave) / 2.0; CGPoint topLeft = CGPointMake([self InsetLeft], [self InsetTop] - [[self LabelFont] lineHeight] + (lineOffset * [[self LabelFont] lineHeight])); // Split the text into hard-split lines (actual &lt;br&gt; tags in the text) NSArray *lines = [[self LabelText] componentsSeparatedByString:@"&lt;br&gt;"]; // Iterate through each hard-coded line for (NSString *line in lines) { // Iterate to the next line topLeft = CGPointMake([self InsetLeft], topLeft.y + [[self LabelFont] lineHeight]); NSArray *pieces = [line componentsSeparatedByString:@"&lt;b&gt;"]; BOOL bold = YES; for (NSString *piece in pieces) { bold = !bold; UIFont *fontToUse; if (bold) { fontToUse = [UIFont boldSystemFontOfSize:[[self LabelFont] pointSize]]; } else { fontToUse = [UIFont systemFontOfSize:[[self LabelFont] pointSize]]; } NSArray *words = [piece componentsSeparatedByString:@" "]; for (NSString *word in words) { if ([word isEqualToString:@""]) continue; NSString *wordWithSpace = [NSString stringWithFormat:@"%@ ", word]; CGSize wordSize = [wordWithSpace sizeWithFont:fontToUse]; if ((topLeft.x + wordSize.width) &gt; (rect.size.width - [self InsetLeft])) { // This runs off this line, so go to the next line topLeft = CGPointMake([self InsetLeft], topLeft.y + [[self LabelFont] lineHeight]); } [wordWithSpace drawAtPoint:topLeft withFont:fontToUse]; topLeft = CGPointMake(topLeft.x + wordSize.width, topLeft.y); } } } } - (NSInteger)numberOfLinesForRect:(CGRect)rect { int retVal = 0; int left = [self InsetLeft]; NSArray *lines = [[self LabelText] componentsSeparatedByString:@"&lt;br&gt;"]; // Iterate through each hard-coded line for (NSString *line in lines) { // Iterate to the next line retVal = retVal + 1; left = [self InsetLeft]; NSArray *pieces = [line componentsSeparatedByString:@"&lt;b&gt;"]; BOOL bold = YES; for (NSString *piece in pieces) { bold = !bold; UIFont *fontToUse; if (bold) { fontToUse = [UIFont boldSystemFontOfSize:[[self LabelFont] pointSize]]; } else { fontToUse = [UIFont systemFontOfSize:[[self LabelFont] pointSize]]; } NSArray *words = [piece componentsSeparatedByString:@" "]; for (NSString *word in words) { if ([word isEqualToString:@""]) continue; NSString *wordWithSpace = [NSString stringWithFormat:@"%@ ", word]; CGSize wordSize = [wordWithSpace sizeWithFont:fontToUse]; if ((left + wordSize.width) &gt; (rect.size.width - [self InsetLeft])) { // This runs off this line, so go to the next line retVal = retVal + 1; left = [self InsetLeft]; } left = left + wordSize.width; } } } return retVal; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload