macOS開發學習筆記(4)

繪製矩形,線條,橢圓,填充顏色,定點寫入字符串

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
    //設置4個點
    NSPoint bottom=NSMakePoint((dirtyRect.size.width/2), 0);
    NSPoint top=NSMakePoint((dirtyRect.size.width/2), dirtyRect.size.height);
    NSPoint left=NSMakePoint(0, (dirtyRect.size.height/2));
    NSPoint right=NSMakePoint(dirtyRect.size.width, dirtyRect.size.height/2);
    
    //設置填充顏色白色
    [[NSColor whiteColor] set];
    [NSBezierPath fillRect:dirtyRect];
    
    //[[NSColor colorWithCalibratedRed:0.749 green:0.749 blue:0.749 alpha:1] set];
    //NSRectFill(dirtyRect);
    
    //繪製3條線條
    [[NSColor blackColor] set];
    [NSBezierPath setDefaultLineWidth:4.5];
    [NSBezierPath strokeRect:dirtyRect];
    [NSBezierPath strokeLineFromPoint:top toPoint:bottom];
    [NSBezierPath strokeLineFromPoint:left toPoint:right];
    [NSBezierPath setDefaultLineWidth:1.5];
    [[NSBezierPath bezierPathWithOvalInRect:dirtyRect] stroke];
    
    //繪製字符串
    NSMutableDictionary *attribs=[NSMutableDictionary dictionary];
    [attribs setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
    
    NSString* end=@"test";
    [end drawAtPoint:NSMakePoint(dirtyRect.size.width/2, dirtyRect.size.height/2)
      withAttributes:attribs];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章