2つのを含むビューがありますNSTextFieldCell
。これらのセルが描画されるサイズは、ビューのサイズから導出されます。各セルのテキストは、導出されたセルのサイズに収まる最大のものにする必要があります。フォントサイズを設定しない私が持っているものは次のとおりです。
- (void)drawRect:(NSRect)dirtyRect {
/*
* Observant readers will notice that I update the whole view here. If
* there is a perceived performance problem, then I'll switch to just
* updating the dirty rect.
*/
NSRect boundsRect = self.bounds;
const CGFloat monthHeight = 0.25 * boundsRect.size.height;
NSRect monthRect = NSMakeRect(boundsRect.origin.x,
boundsRect.origin.y + boundsRect.size.height
- monthHeight,
boundsRect.size.width,
monthHeight);
[monthCell drawWithFrame: monthRect inView: self];
NSRect dayRect = NSMakeRect(boundsRect.origin.x,
boundsRect.origin.y,
boundsRect.size.width,
boundsRect.size.height - monthHeight);
[dayCell drawWithFrame: dayRect inView: self];
[[NSColor blackColor] set];
[NSBezierPath strokeRect: boundsRect];
}
したがって、特定の属性に必要なサイズを文字列に要求できること、およびコンテンツに合わせてサイズを変更するようにコントロールに要求できることを知っています。どちらも当てはまらないようです。コンテンツ(この場合はセルstringValue
)のサイズを既知の長方形の寸法に合わせ、それを実現するために必要な属性を不明にします。必要なサイズを見つけるにはどうすればよいですか?使用するフォントがわかっていると仮定します(使用しているため)。
更新注:文字列を切り捨てたくはありません。文字列を拡大または縮小して、提供されたrectに可能な限り最大のテキストサイズで全体が収まるようにします。