代码之家  ›  专栏  ›  技术社区  ›  Madhup Singh Yadav

绘制带下划线/删除线的文本(多行字符串)?

  •  5
  • Madhup Singh Yadav  · 技术社区  · 16 年前

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

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

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

    但所有的文字只画一行。而我有多行文字。当文本对齐居中时,情况甚至变得更糟。我搜索后发现,在iphone-sdk-3.2中,有一些核心文本属性用于在文本下划线,但不知道如何使用。另外,如果我用这些,我的问题就不会完全解决。

    因为我还要画删除线文本。

    有人知道了请帮忙。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Madhup Singh Yadav    16 年前

    这个怎么样。这对我很好,你们都这么认为,如果这需要一些优化?

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

    希望这对大家都有好处。

    谢谢,

    马杜普

        2
  •  1
  •   Peter Hosey    16 年前

    使用核心文本。

    我搜索后发现,在iphone-sdk-3.2中,有一些核心文本属性用于在文本下划线,但不知道如何使用。

    就是这样 the documentation 是为了。