NSString drawAtPoint:withAttributes:gap

时间:2017-06-05 03:37:40

标签: cocoa text fonts

请参阅以下代码和两张图片:

- (void)drawRect:(NSRect)dirtyRect {

NSMutableDictionary* dicAttributes = [NSMutableDictionary dictionary];

[dicAttributes setObject:[NSFont fontWithName:@"Tahoma" size:18] forKey:NSFontAttributeName];
[dicAttributes setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];

NSString* text = @"111111";
NSSize size = [text sizeWithAttributes:dicAttributes];
[[NSColor grayColor] set];
NSRectFill(NSMakeRect(0, 0, size.width, size.height));
[text drawAtPoint:NSMakePoint(0, 0) withAttributes:dicAttributes];
NSLog(@"---------size(%f, %f)", size.width, size.height);

text = @"111\n111";
size = [text sizeWithAttributes:dicAttributes];
[[NSColor grayColor] set];
NSRectFill(NSMakeRect(100, 0, size.width, size.height));
[text drawAtPoint:NSMakePoint(100, 0) withAttributes:dicAttributes];
NSLog(@"---------size(%f, %f)", size.width, size.height);
}

- (BOOL)isFlipped{
return YES;
}
  1. 这是原始截图。

    enter image description here

  2. 这是我修改过的截图,我需要图片中绿色和黄色部分的高度。

    enter image description here

0 个答案:

没有答案