Quartz 2d / Core Graphics: What is the right way to draw some text?
I’ve been at this for awhile, it seems that there’s many ways to go about this in quartz 2d:
1) Draw text using core graphics methods…
CGContextSelectFont CGContextSetRGBFillColor CGContextShowTextAtPoint
and on and on, which is horribly low level.
2) using NSString drawAtPoint
(so far the method I like)
NSString* text = @"Hello"; [text drawAtPoint:point withFont:font];
3) using UILabel
I’ve read this somewhere but not too sure if this is possible. but I’m thinking that instantiating a UILabel within drawRect would be pretty costly as drawRect probably gets called a zillion times. ??
I’m doing ok with 2. (using NSString drawAtPoint) for the moment but I wanted to get some opinions. Again, I’m trying to create text within drawRect (I subclassed a view) because I’m also drawing shapes along with text, what is the right way?
3 Solutions Collect From Internet About “Quartz 2d / Core Graphics: What is the right way to draw some text?”
#1 doesn’t support international characters. So, don’t use it if you use it to draw texts inputted by a user.
So, please use #2 unless you’re absolutely sure that the text only contains standard ASCII characters.
#2 is how I generally do it unless I need the more precise control of using CG/CT
While I agree with Yuji that you should not use #1, I would add that if at all possible you should use a UILabel instead of custom drawing, because if you can avoid implementing -drawRect: at all, anywhere, you should.
- ERROR | file patterns: The `source_files` pattern did not match any file
- iOS: reload single cell of UICollectionView
- SUBQUERY in NSPredicate and NSInvalidArgumentException
- How do I implement AudioServicesSystemSoundCompletionProc in Swift?
- Reloading UICollectionView causing flickering
- Drawing particles
- UDP broadcast using GCDAsyncUdpSocket through all available Network Interfaces
- 'Cannot do a comparison query for type: (null)' when setting up UINavigationController
- Draw gradient along a curved UIBezierPath
- Reverse an audio file Swift/Objective-C
- Does @“some text” give an autoreleased or retain 1 object back?
- UITextField settext not working
- Xcode6 can't run any app in simulator ( Domain = NSPOSIXErrorDomain, Code = 3 )
- ios metal: limit on number of variables used in a shader
- Correct implementation of parent/child NSManagedObjectContext