2014-01-19 25 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