设置颜色CFAttributedStringRef
我有这种方法来绘制一个表蚂蚁填充。我想要的是改变每一栏的一个词的颜色,但我不知道我该怎么做。有人可以帮我吗?任何帮助将不胜感激。设置颜色CFAttributedStringRef
在我的应用程序,用户可以选择从segmente控制某些属性...我想他导出所选的内容在PDF,像一张桌子。所以在每一行字将被选中
-(void)drawTableDataAt:(CGPoint)origin
withRowHeight:(int)rowHeight
andColumnWidth:(int)columnWidth
andRowCount:(int)numberOfRows
andColumnCount:(int)numberOfColumns
{
int padding = 1;
NSArray* headers = [NSArray arrayWithObjects:@"Grand", @"Taile ok", @"Petit", nil];
NSArray* invoiceInfo1 = [NSArray arrayWithObjects:@"Extra", @"Bon", @"Ordi", nil];
NSArray* invoiceInfo2 = [NSArray arrayWithObjects:@"Gras", @"Etat", @"Maigre", nil];
NSArray* invoiceInfo3 = [NSArray arrayWithObjects:@"Cru", @"Propre", @"Sale", nil];
NSArray* invoiceInfo4 = [NSArray arrayWithObjects:@"PLourd", @"PMoyen", @"PLeger", nil];
NSArray* invoiceInfo5 = [NSArray arrayWithObjects:@"CSup", @"CEgal", @"CInf", nil];
NSArray* allInfo = [NSArray arrayWithObjects:headers, invoiceInfo1, invoiceInfo2, invoiceInfo3, invoiceInfo4, invoiceInfo5,nil];
for(int i = 0; i < [allInfo count]; i++)
{
NSArray* infoToDraw = [allInfo objectAtIndex:i];
for (int j = 0; j < numberOfColumns; j++)
{
int newOriginX = origin.x + (j*columnWidth);
int newOriginY = origin.y + ((i+1)*rowHeight);
CGRect frame = CGRectMake(newOriginX + padding, newOriginY + padding, columnWidth, rowHeight);
[self drawText:[infoToDraw objectAtIndex:j] inFrame:frame];
}
}
}
- (无效)的drawText:(的NSString *)textToDraw框内:(的CGRect)frameRect {
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
}
根据对这个问题的评论,你提到这些词永远不会改变。你可能会创建一大堆if/else语句来检查每个选中的单词对数组中的每个单词。我已经把它作为一个更有效的选择,它应该有希望工作。它可能需要一些调整,甚至一个循环来遍历您选择的话,但这应该有希望把你在正确的方向:
//declare your textToDraw as a new NSString
NSString *str = textToDraw;
//Make an Array of the str by adding objects that are separated by whitespace
NSArray *words = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//create a BOOL to check if your selected word exists in the array
BOOL wordExists = [words containsObject: @"%@", yourSelectedWord];
CTFramesetterRef framesetter = null;
//if the word exists, make it red
if(wordExists){
NSUInteger indexOfTheString = [words indexOfObject: @"%@", yourSelectedWord];
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL,str, NULL);
[currentText addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(indexOfTheString, yourSelectedWord.length)];
framesetter = CTFramesetterCreateWithAttributedString(currentText);
}
这将匹配你的阵列,突出对正确的字找到您所选择的字它是红色的。
这应该在哪里实施?对不起,但这是第一次当我做这样的事情时:( –
在你的drawText方法中,我以前没有尝试过,所以它可能需要一些玩弄,但理论上它应该可以工作,我想你可能已经完成了你的布局更难处理,但我希望这可以工作。 –
和标签在哪里?因为我不使用标签.... –
那么每列的颜色或? –
每行一个单元格的颜色。例如,我想让下一个单元变成红色:Grand,Bon,Gras,Sale,Pleger和CEgal。 –
那么基本上这个单词会是随机的,并且有一套颜色?这份名单会不会改变? –