I am trying to draw a block of text with a stroke. So doing a bit of searching and experimenting seems that the way to do it is to subclass UIView and draw the text with a stroke in drawRect. The drawing code is below.
The way I have it set up, I am using a UITextView to enter the text I want interactively, then the text is passed to the UIView subclass ad drawn with a stroke.
This seems to be working perfectly. The only problem is when the return key is pressed (creating a new line), or the line wraps because it becomes too wide for the UITextView, this information is not passed, so there is no new line in my renderd (stroked) text drawn in the UIView subclass.
Any help?
Drawing code (note that theText is a property to pass the UITextView’s text to):
UIFont *font = [UIFont fontWithName:@"Arial" size:fontSize];
CGPoint point = CGPointMake(0,0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 4.0);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSaveGState(context);
[theText drawAtPoint:point withFont:font];
CGContextRestoreGState(context);
You must duplicate the wrapping logic of uitextview. Ask the string how long the substring needs to be, and when it gets to long to fit its going to wrap.
You can also look at DTCoreText, a class that lets you render attributed text. However the version that mimics textviews and supports text entry costs something. In the end that cost may be worth it as duplicating a textview will take time .