diff --git a/darwin/drawtext.m b/darwin/drawtext.m index 6206c810..e56e1763 100644 --- a/darwin/drawtext.m +++ b/darwin/drawtext.m @@ -600,3 +600,28 @@ void doDrawText(CGContextRef c, CGFloat cheight, double x, double y, uiDrawTextL y -= yoff; CGContextSetTextPosition(c, x, y); #endif + +// TODO length - 1? +#define rangeToCFRange() CFRangeMake(startChar, endChar - startChar) + +void uiDrawTextLayoutSetColor(uiDrawTextLayout *layout, intmax_t startChar, intmax_t endChar, double r, double g, double b, double a) +{ + CGColorSpaceRef colorspace; + CGFloat components[4]; + CGColorRef color; + + // for consistency with windows, use sRGB + colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); + components[0] = r; + components[1] = g; + components[2] = b; + components[3] = a; + color = CGColorCreate(colorspace, components); + CGColorSpaceRelease(colorspace); + + CFAttributedStringSetAttribute(layout->mas, + rangeToCFRange(), + kCTForegroundColorAttributeName, + color); + CGColorRelease(color); // TODO safe? +} diff --git a/test/page10.c b/test/page10.c index b887af02..db104b93 100644 --- a/test/page10.c +++ b/test/page10.c @@ -30,7 +30,6 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *dp) // TODO memset(&desc, 0, sizeof (uiDrawTextFontDescriptor)); - family = uiEntryText(textFont); desc.Family = "Arial"; desc.Size = 36; desc.Weight = uiDrawTextWeightNormal; @@ -38,11 +37,11 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *dp) desc.Stretch = uiDrawTextStretchNormal; font = uiDrawLoadClosestFont(&desc); - layout = uiDrawNewTextLayout(s, "One two three four", -1); - uiDrawTextLayoutSetForegroundColor(layout, + layout = uiDrawNewTextLayout("One two three four", font, -1); + uiDrawTextLayoutSetColor(layout, 4, 7, 1, 0, 0, 1); - uiDrawTextLayoutSetForegroundColor(layout, + uiDrawTextLayoutSetColor(layout, 8, 14, 1, 0, 0.5, 0.5); uiDrawText(dp->Context, 10, 10, layout);