gpt4 book ai didi

ios - 设置颜色 CFAttributedStringRef

转载 作者:行者123 更新时间:2023-11-29 12:19:44 25 4
gpt4 key购买 nike

我有这个方法来绘制一个 Ant 填充的表格。我想要的是改变每一列中一个单词的颜色,但我不知道该怎么做。有朝一日可以帮帮我吗?任何帮助将不胜感激。

在我的应用程序中,用户可以从 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];
}

}

}

-(void)drawText:(NSString*)textToDraw inFrame:(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);
}

enter image description here enter image description here

最佳答案

根据对问题的评论,您提到这些词永远不会改变。您可能会创建一大堆 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);

}

这会将找到的所选单词与数组中的正确单词进行匹配,并将其突出显示为红色。

关于ios - 设置颜色 CFAttributedStringRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30978231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com