请参阅以下代码和两张图片:
- (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;
}
这是原始截图。
这是我修改过的截图,我需要图片中绿色和黄色部分的高度。