5

I have to draw underlined-multiline text with all types of text alignment. I have searched on forums and got some results like:

http://davidjhinson.wordpress.com/2009/11/26/underline-text-on-the-iphone/

http://forums.macrumors.com/showthread.php?t=561572

But all draw text for single line only. while i have multi-line text. The situation even become worse when the text alignment is centered. I searched and found that in iphone-sdk-3.2 there are some core-text attributes for underlining a text but no idea how to use that. Besides if I use these my problem would not be solved fully.

As I have to draw strikethrough text also.

Anybody having idea about this please help.

Peter Hosey
  • 93,914
  • 14
  • 203
  • 366
Madhup Singh Yadav
  • 8,077
  • 7
  • 48
  • 82

2 Answers2

2

What about this. This works good for me what you all think, if this needs some optimisation?

CGContextSetFillColorWithColor(c, [[UIColor blackColor] CGColor]);          //THE TEXT COLOR OF THE STRING TO BE DRAWN.

            UIFont *fontName = [UIFont fontWithStyle:@"Arial-BoldMT" andFontSize:13];

            if(FontOverLayUnderLine || FontOverLayStrikeThrough){

                NSArray *stringsArray = [textString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
                CGContextSetStrokeColorWithColor(c, [appDel.textDisplayStyle.fillColor CGColor]);
                CGContextSetLineWidth(c, 1.0);
                CGSize stringSize;
                NSString *stringToDraw;
                for (int i = 0 ; i < [stringsArray count]; i++) {

                    stringToDraw = [stringsArray objectAtIndex:i];

                    if(![[stringToDraw stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]){

                        stringSize = [stringToDraw sizeWithFont:fontName forWidth:rect.size.width lineBreakMode:UILineBreakModeCharacterWrap];

                        float x = rect.origin.x + (rect.size.width-stringSize.width)/2 + 7;
                        float y = 4 + stringSize.height*i;

                        [stringToDraw drawAtPoint:CGPointMake(x, y) forWidth:stringSize.width withFont:fontName lineBreakMode:UILineBreakModeCharacterWrap];

                        if(FontOverLayUnderLine)
                            y += (1.05) * stringSize.height*i +1;
                        else
                            y += (stringSize.height/2)+1;

                        CGContextMoveToPoint(c, x, y);
                        CGContextAddLineToPoint(c, x+stringSize.width, y);
                        CGContextStrokePath(c);
                    }
                }

            }

Hope this works good for all.

Thanks,

Madhup

Madhup Singh Yadav
  • 8,077
  • 7
  • 48
  • 82
  • Hi Madhup, this doesnt work for me. Please Can you please help me with multiline text underline in a UILabel. It would be great if you can do so. – Parth Bhatt Sep 09 '11 at 05:31
  • @Parth\ Bhatt the code above is for struckthrough text. You should try to change the frame of line a little bit to get underlined text. Please see the first link in my question itself. – Madhup Singh Yadav Sep 09 '11 at 07:12
  • It doesn't show underline for all the lines of the multiline text. I have multiline label – Parth Bhatt Sep 09 '11 at 09:45
  • In case of multiline label you should break the whole text manually into separate lines putting a newline character. then above code will work smoothly. This is what I think, for further help you can always start a fresh bounty on this question or new question mentioning your requirements. – Madhup Singh Yadav Sep 09 '11 at 10:51
1

Use Core Text.

I searched and found that in iphone-sdk-3.2 there are some core-text attributes for underlining a text but no idea how to use that.

That's what the documentation is for.

Peter Hosey
  • 93,914
  • 14
  • 203
  • 366