I'm having trouble drawing an NSAttributedString
with no margins around it in its view. Here's my code:
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:72.0f]};
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Hello"
attributes:attributes];
[string drawWithRect:rect
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics
context:nil];
This leads to the behavior below:
Note the margins at the left and top.
- How can I avoid these margins and make the text exactly meet the edges of the containing view? What I want is for the actual drawn text to meet the edges of the view, that is, the topmost pixel drawn in any glyph should be at the top of the view, and the leftmost pixel drawn in any glyph is at the left side of the view.
- Assuming 1 is possible, is there a way I can get the actual width and height of the drawn text so that I can calculate font size and kerning to make the text meet the bottom and right edges as well?
I realize that there may be ways to align text without making it meet the edges of the view, but making it meet the edges of the view will allow me to work with the view intuitively using autolayout and so on.
I do not care about the behavior when the string contains leading or trailing whitespace.
If this is not possible with NSAttributedString
, are there other ways to get this behavior that you'd recommend?
To clarify, here's what I want to see for number 1.