мой код:
- (void)drawRect:(CGRect)rect {
/* Define some defaults */
float padding = 0.0f;
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef) [UIFont systemFontOfSize:12].fontName, [UIFont systemFontOfSize:12].lineHeight, NULL);
/* Get the graphics context for drawing */
CGContextRef ctx = UIGraphicsGetCurrentContext();
/* Core Text Coordinate System is OSX style */
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
CGContextTranslateCTM(ctx, 0, self.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
//CGRect textRect = CGRectMake(padding, padding, self.frame.size.width, self.frame.size.height);
CGRect textRect = CGRectMake(padding, padding, 100, 100);
/* Create a path to draw in and add our text path */
CGMutablePathRef pathToRenderIn = CGPathCreateMutable();
CGPathAddRect(pathToRenderIn, NULL, textRect);
NSLog(@"textRect:%@",NSStringFromCGRect(self.bounds));
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"hello world!"];
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTFontAttributeName, font);
/* Get a framesetter to draw the actual text */
CTFramesetterRef fs = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) attrString);
CTFrameRef frame = CTFramesetterCreateFrame(fs, CFRangeMake(0, attrString.length), pathToRenderIn, NULL);
/* Draw the text */
CTFrameDraw(frame, ctx);
/* Release the stuff we used */
CFRelease(frame);
CFRelease(pathToRenderIn);
CFRelease(fs);
}
Однако это выглядит так: как сделать так, чтобы текст начинался с верхнего левого угла и не изменял текстовый фрейм (ширина = 100 и высота = 100)?
xxxxxxxxxxxxx x x x x x x x x x x xtext... x x x xxxxxxxxxxxxx