CoreText - Font color is always ignored

I'm trying to draw an Unicode string using Core Text without creating a new CTLine(attributeedString) each time, because I need to draw hundreds of strings quite frequently. The problem is that the color defined in CTStringAttributes is not respected: None of those two attempts work, the text color is always BLACK:

var attributedString = new NSAttributedString(
                "ABCD",
                attr = new CTStringAttributes
                {
                    ForegroundColorFromContext = false,
                    ForegroundColor = UIColor.Blue.CGColor,
                    Font = new CTFont("Arial", 24)
                });

attributedString.DrawString(new CGPoint());//draws in BLACK instead  of BLUE

Or

gctx.SetFillColor(UIColor.Blue.CGColor);

attributedString = new NSAttributedString(
               "ABCD",
               attr = new CTStringAttributes
               {
                   ForegroundColorFromContext = true,
                   Font = new CTFont("Arial", 24)
               });

  attributedString.DrawString(new CGPoint());//draws in BLACK instead  of BLUE

Any ideas ?

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Why are you drawing the text instead of using a UILabel?

  • NorbertHesterNorbertHester USMember
    edited September 2015

    @adamkemp: because I do complete custom drawing by subclassing an UIView and override its draw(...).
    I'm searching for an efficient way to draw many text parts in a custom drawer (scrollable, zoomable). The texts are not known in advance.
    While the "Font" property is respected, the ForegroundColor[FromContext] property is not. So it seems to be a bug either in iOS or in Xamarin.iOS...?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    First of all, using DrawString is not "CoreText". CoreText is a much lower level API. You're just using CoreGraphics. Second, I hate to be this guy, but in my experience most people who think they need custom drawing are wrong so I have to ask again, why are you doing custom drawing? Are you sure you need custom drawing?

    Third, your quoted code doesn't even compile so I'm not sure what you're actually trying. Try to be more careful when posting code because when people have to make assumptions about what you're actually doing it can lead them astray.

    Regardless, since you're not actually using CoreText the solution seems to be to use UIStringAttributes instead of CTStringAttributes:

                var attributedString = new NSAttributedString(
                    "ABCD",
                    attributes: new UIStringAttributes
                    {
                        ForegroundColor = UIColor.Blue,
                        Font = UIFont.FromName("Arial", 24)
                    });
    

    That drew blue text for me.

  • NorbertHesterNorbertHester USMember
    edited September 2015

    Thanks for pointing out that it is not CoreText and your code fix !
    But my code compiles, at least on my machine (I've even copied it from this post back into VS - no problem).
    And in my case I really need custom drawing, as I'm also drawing some simple shapes and small images. Just did not mention it as it is not relevant for this problem...
    Btw.: I think Core Text is higher level than Core Graphics, not lower level: "Core Text is designed for development of higher-level text-handling...Core Text works directly with Core Graphics (CG), also known as Quartz, which is the high-speed graphics rendering engine " (from https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/CoreText_Programming/Overview/Overview.html

  • adamkempadamkemp USInsider, Developer Group Leader mod

    my code compiles, at least on my machine

    Are you sure you're compiling the exact code you quoted? Which version of Xamarin.iOS are you using?

    The specific thing that didn't compile is the attr = ... part. That's not even valid C# syntax. The closest valid syntax would be attr: ..., but that's not valid either because the name of the argument is "attributes", not "attr".

    I think Core Text is higher level than Core Graphics, not lower level...

    I guess you're right that it's "higher level", but it's for doing much more complex things than just drawing a line of text. If you're doing complex text layout then you might need it, but if you just want to draw a line of text you really don't. I was conflating "advanced" with "low level", and that's not accurate. My mistake.

  • Well it compiles on my machine, because I defined a field "attr" in my subclassed UIView in order to reuse it (had overlooked that while posting it).
    But unfortunately, as I need to draw Unicode text and CG only supports pure ascii chars, I will probably have to use the way described here (creating a new CTLine for each text part, which I wanted to avoid because I suspect that it will have a performance impact):https://developer.xamarin.com/recipes/ios/graphics_and_drawing/core_text/draw_unicode_text_with_coretext/

  • CheesebaronCheesebaron DKInsider, University mod

    I use CTLine in my own chart component even if there are 100 calls to CTLine it performs pretty fast. So I wouldn't worry to much about it.

Sign In or Register to comment.