我正在尝试用不同颜色绘制文本的某些部分
首先使用以下代码时发出:
NSMutableAttributedString *test1 = [[NSMutableAttributedString alloc] initWithString:@"fi"];
// Should color only the first character "f"
[test1 addAttribute:(id)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:NSMakeRange(0, 1)];
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)test1);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CTFrameRef textFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, test1.string.length), path, NULL);
然后绘制框架:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
// Flip the coordinate system
CGContextScaleCTM(context, 1.0, -1.0);
CTFrameDraw(textFrame, context);
CGContextRestoreGState(context);
由于某种原因,它显示“f”和“i”都为红色。当我将文本更改为“f1”、“f!”或其他任何内容时,它工作正常。即使当我使用“afi”并将范围设置为 (1,1) 时,它也会对“f”和“i”着色,就好像将其视为单个字符一样。
第二个问题,当在宽度小于文本宽度的框架内绘制时:
NSMutableAttributedString *test2 = [[NSMutableAttributedString alloc] initWithString:@"aaaaaaaaaaaaaaaaaaaa"];
CTLineBreakMode lineBreakMode;
lineBreakMode = kCTLineBreakByTruncatingTail;
// Apply selected truncation
CTParagraphStyleSetting paragraphStyleSetting = {kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &lineBreakMode};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(¶graphStyleSetting, 1);
[test1 addAttribute:(NSString *)kCTParagraphStyleAttributeName value:(__bridge id)paragraphStyle range:NSMakeRange(0, test2.string.length)];
[test1 addAttribute:(id)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:NSMakeRange(7, 16)];
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)test2);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CTFrameRef textFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, test1.string.length), path, NULL);
共有20个字符, 索引 7 到 16 处的字符显示为红色(正如它们应该的那样),CoreText 按照我的要求添加了截断标记,但问题是它也显示为红色,我希望它保持黑色(因为剩余的截断字符颜色为黑色)。
我做了一些研究,似乎当通过命令中的某个(随机/字体特定?)值增加帧大小时:
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width + 10.0f, self.frame.size.height));
它使截断标记保持黑色,但由于额外的空格,有时截断标记会被剪掉
最佳答案
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
关于iPhone SDK : NSMutableAttributedString buggy behavior when using CTFramesetterCreateFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9003666/