我必须绘制带有所有类型的文本对齐方式的带下划线的多行文本。我在论坛上进行了搜索,结果如下:

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

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

但是所有的绘制文本仅用于单行。虽然我有多行文字。当文本对齐居中时,情况甚至变得更糟。我搜索后发现,在iphone-sdk-3.2中,有一些用于文本下划线的核心文本属性,但不知道如何使用。此外,如果我使用这些工具,我的问题将无法完全解决。

由于我还必须绘制删除线文本。

任何对此有想法的人请帮忙。

最佳答案

那这个呢。如果您需要一些优化,这对我有用,你们大家都认为呢?

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);
                    }
                }

            }

希望这对所有人都有好处。

谢谢,

马杜普

关于iphone - 绘制带下划线/删除线的文本(MULTILINE STRING)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2652163/

10-12 00:18
查看更多