2014-01-19 23 views
2

这是我第一次使用核心图形和文本,因此可能会做一些非常简单的事情,这是不正确的。我一直在阅读文档,试图看看我哪里出错,但看不到它,所以不知道是否有人可以帮我找出问题。核心文本 - 文本的位置不正确

我想在屏幕顶部绘制一个简单的小横幅信息,但是我想在横幅中显示的文本并未绘制在正确的位置。

在我看来,我创建了我的控制如下:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.banner = [[Banner alloc] initWithFrame:CGRectMake(0,20,768,200)]; 
    [self.view addSubview:self.banner]; 
} 

控件代码如下:

#import <CoreText/CoreText.h> 

#import "Banner.h" 

@implementation Banner 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1]; 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Get the graphics context. 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Draw the banner border. 
    CGContextSaveGState(context); 

    UIColor *strokeColor = [UIColor blackColor]; 
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor); 
    CGContextSetLineWidth(context, 5); 
    CGContextStrokeRect(context, CGRectMake(2,2,self.frame.size.width -4,100)); 

    CGContextRestoreGState(context); 


    // Setup the text label & value. 
    NSString *addressLabelText = @"Address"; 
    NSString *addressValue = @"123 Letsby Avenue\nSome Place\nSome City\nSome County\nSome Postcode"; 

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:14.0]; 

    CGSize labelSize = [self getBoundsForString:addressLabelText usingFont:font]; 
    CGSize valueSize = [self getBoundsForString:addressValue usingFont:font]; 

    // So I want to Draw the label at X:10 Y:10 and the Value At X:65 Y:10 
    // So we get Something like: 
    // ============================= 
    // || Address 123 letsby avenue 
    // ||   Some Place 
    // ||   Some City 
    // ||   Some County 
    // ||   Some Postcode 
    // ============================= 

    CGRect labelBounds = CGRectMake(10,10,labelSize.width,labelSize.height); 
    // Move the address value over the margin width(10) + the label width + some spacing(5) 
    CGRect valueBounds = CGRectMake(10 + labelSize.width + 5, 10, valueSize.width, valueSize.height); 

    [self drawString: addressLabelText withFont: font inRect:labelBounds usingContext:context]; 
    [self drawString: addressValue withFont: font inRect:valueBounds usingContext:context]; 

    // The text hasn't drawn where i thought it should. Draw a rect there to make sure i had the right bounds 
    UIColor *rectFillColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.3]; 

    CGContextSaveGState(context); 

    CGContextSetFillColorWithColor(context, [rectFillColor CGColor]); 
    CGContextFillRect(context, labelBounds); 
    CGContextFillRect(context, valueBounds); 

    CGContextRestoreGState(context); 
    //Hmm that is drawing in the right place. Whats going wrong with drawing the text. 

    [self setNeedsDisplay]; 

} 

-(void) drawString:(NSString*) string withFont:(UIFont*)font inRect:(CGRect)rect usingContext:(CGContextRef) context 
{ 
    CGContextSaveGState(context); 

    // flipping the coordinate system. 
    CGContextTranslateCTM(context, 0, 200); // The control is rendered in a frame 200 high. 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 

    // Define the path we are going to draw the text on. 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddRect(path, NULL, rect); 

    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef)string); 

    // Create the framesetter with the attributed string. 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString); 
    CFRelease(attrString); 

    // Create a frame. 
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 

    // Draw the specified frame in the given context. 
    CTFrameDraw(frame, context); 

    CGContextRestoreGState(context); 

    CFRelease(frame); 
    CFRelease(path); 
    CFRelease(framesetter); 
} 

-(CGSize) getBoundsForString:(NSString *)string usingFont:(UIFont *)font 
{ 
    return [string boundingRectWithSize:CGSizeMake(999,999) 
        options:NSStringDrawingUsesLineFragmentOrigin 
        attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] 
        context:nil].size; 
} 


@end 

这样做的结果是横幅广告的画一些蓝色rects我期望的文字是,但文字实际上出现在横幅以外,我很困惑为什么。

https://www.dropbox.com/s/q2ap9tl4okaka49/Banner.png

回答

2

我发现了似乎为我的代码工作上面......改变这种:

-(void) drawString:(NSString*) string withFont:(UIFont*)font inRect:(CGRect)rect usingContext:(CGContextRef) context 
{ 
    CGContextSaveGState(context); 

    // flipping the coordinate system. 
    CGContextTranslateCTM(context, 0, 200); // The control is rendered in a frame 200 high. 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 

-(void) drawString:(NSString*) string withFont:(UIFont*)font inRect:(CGRect)rect usingContext:(CGContextRef) context 
{ 
    CGContextSaveGState(context); 

    // flipping the coordinate system. 
    CGContextTranslateCTM(context, 0, rect.size.height+(2*rect.origin.y)); 
    CGContextScaleCTM(context, 1.0, -1.0); 

线以下行似乎并不改变任何事情。

CGContextSetTextMatrix(context, CGAffineTransformIdentity); 

而改变这一行:文本正确

CGContextTranslateCTM(context, 0, 200); 

CGContextTranslateCTM(context, 0, rect.size.height+(2*rect.origin.y)); 

地方,我希望它是....使用Xamarin和等价的C#我必须反转高度+ 2Y结果的结果:-S