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

发布于 2024-08-28 18:20:11 字数 580 浏览 1 评论 0原文

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

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

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

但都只绘制单行文本。当我有多行文本时。当文本居中对齐时,情况甚至会变得更糟。我搜索并发现在 iphone-sdk-3.2 中有一些用于在文本下划线的 core-text 属性,但不知道如何使用它。此外,如果我使用这些我的问题将无法完全解决。

因为我还必须绘制删除线文本。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2024-09-04 18:20:11

这又怎样呢。大家认为这对我来说很有用,是否需要一些优化?

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

            }

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

谢谢,

马杜普

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

巡山小妖精 2024-09-04 18:20:11

使用核心文本。

我搜索并发现在 iphone-sdk-3.2 中有一些用于为文本添加下划线的 core-text 属性,但不知道如何使用它。

这就是文档 是为了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文