我必须使用所有类型的文本对齐来绘制带下划线的多行文本。我在论坛上搜索了一下,得到了一些结果,比如:
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属性,但不知道如何使用。此外,如果我使用这些,我的问题将不会完全解决。
因为我还必须绘制删除线文本。
任何人对此都有想法,请帮助。
发布于 2010-04-19 14:17:39
那这个呢。如果这需要一些优化,这对我来说很好,你们都这么认为吗?
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);
}
}
}希望这对所有人都有好处。
谢谢,
Madhup
发布于 2010-04-17 03:29:45
使用核心文本。
我搜索了一下,发现在iphone-sdk-3.2中有一些给文本加下划线的核心文本属性,但不知道如何使用。
这就是the documentation的作用。
https://stackoverflow.com/questions/2652163
复制相似问题