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

标签 iphone core-graphics ipad iphone-sdk-3.2 core-text

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

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 属性,但不知道如何使用它。此外,如果我使用这些,我的问题将无法完全解决。

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

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

最佳答案

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

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 - 绘制下划线/删除线文本(多行字符串)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2652163/

相关文章:

iphone - 尝试从 iphone 连接到 node.js 和 socket.io 0.7.2

iphone - CGContextRef 中的坐标系是什么样的?

UISearchBar 范围按钮的 iOS 可视化更改

iphone - 在移动 Safari (html5) 中缓存离线 iPhone/iPad Web 应用程序的视频内容?

iphone - 添加一个 UIButton 到 tableView headerView

ios - 无法在 ios 中获取用户个人资料照片

iphone - iPad:如何将iPhone应用程序转换为iPad兼容?

iphone - 动画 CAShapeLayer 饼图

ios - 缩放 CGPath 以适应 UIVIew

ios - 在 iOS 设备上模拟无服务