Greetings Jason,

On 5/24/12 12:49 AM, Jason Teagle wrote:
> I have need of rendering text manually to a UIView, along with graphics.
> I realise that I could probably use a UITextView as a subview and
> position it, but for now I'd like to learn the right way to render it
> manually.

Please see the last paragraphs below for a discussion of why rendering
manually is almost certainly the wrong way.

You probably want UILabel.  UITextView is a much more heavyweight class
that supports editing as well as display.

For displaying small strings, UILabel is the way to go.

> My aim is to render a particular string in a particular colour, in a
> particular font (or, any Sans Serif of the system's choosing would be
> enough here) and a particular font size.
> 
> First, a quick question about +stringWithFormat: If I want to have a
> literal string as one of the parameters to the format, which is correct
> / best:
> 
> NSString *textToDraw = [NSString stringWithFormat: @"%s",
>     "my string"];
> 
> or
> 
> NSString *textToDraw = [NSString stringWithFormat: @"%@",
>     @"my string"];

These both are just wasting CPU cycles and adding extraneous code.

Simply use:

NSString *textToDraw = @"my string";


> Now, I'm led to believe that you can either use CGContextXXX calls, or
> Cocoa text drawing. I seemed to fall flat on my face with the Cocoa way,
> so I was trying to use this:

Whoa, let's back up here.  There is no way that using Quartz/Core
Graphics is *easier* than using the Cocoa text drawing functionality
(particularly the NSString UIKit additions).  If you "fall flat" using
the latter, you shouldn't even consider making life yet harder for yourself.

What did you try in Cocoa and what were the results?

>                                 CGContextShowTextAtPoint(contextRef, 0, 40,
>     [textToDraw UTF8String], [textToDraw length]);
> 
> (since -cString: is deprecated). But I end up with garbage characters
> and I don't know if I'm using +stringWithFormat: incorrectly, or
> CGContextShowTextAtPoint().
> 
> What is the 'correct' or best recommended way to render text, lines and
> filled primitives? For the Cocoa way of rendering text, how do I control
> font face / size? The sample I found used NSDictionary for the
> attributes but it won't compile the NSFontAttributeName the (web) sample
> used for the key.

NSFontAttributeName is declared in the NSAttributedString *AppKit*
additions, which isn't available on iOS.  Core Text on iOS has
kCTFontAttributeName, but Core Text is a whole other beast that doesn't
really apply to your situation (and is definitely NOT something to
tackle when getting started).

(As a general rule, text presentation is one area where there is little
overlap between iOS and OS X, so it's important to pay close attention
to which platform is targeted by any sample code.)

Back to your problem at hand, if your text snippet is all drawn using
the same font attributes, just use UILabel and position it alongside/on
top of one or more UIImageViews for your graphics.

If you really need to do custom drawing for some other purpose, use the
NSString UIKit additions.  There is *very* little reason in most cases
to start messing with CGContextShowTextAtPoint() and friends.

Why do I recommend avoiding your own drawing entirely if possible?  In
addition to adding lots more work (and confusion) for yourself by
tackling explicit drawing, you are going to introduce performance
bottlenecks, especially when you start interacting with, say, a
UIScrollView.  The UIKit classes have been highly optimized for their
purpose-built tasks, and getting such performance out of your own
drawing code can be quite challenging.

(Oh, and for text in particular, you also lose important features like
text selection that the framework classes provide!)

In Cocoa, you should always try to accomplish a task through class
composition rather than subclassing (which is of course required for
custom drawing).  Probably 90% of what people start doing in -drawRect:
can be done by appropriate composition of framework classes.

-- 
Conrad Shultz

Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to