
- (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.height - monthHeight); [dayCell drawWithFrame: dayRect inVIEw: self]; [[NScolor blackcolor] set]; [NSBezIErPath strokeRect: boundsRect];} 所以我知道我可以问一个字符串给定属性需要的大小,我知道我可以让控件改变它的大小以适应它的内容.这些都不适用:我希望内容(在这种情况下,单元格的stringValue)的大小适合已知的rect维度,以及实现未知的所需属性.如何找到所需的尺寸?假设我知道我将使用什么字体(因为我这样做).
更新注意:我不想截断字符串,我想增长或缩小它,以便整个事物适合,尽可能大的文本大小,提供给提供的矩形.
解决方法 我使用一些类似的代码,但它处理不同的字体,大小高达10,000,并考虑到可用的高度以及文本显示区域的宽度.#define kMaxFontSize 10000- (CGfloat)FontSizeforAreaSize:(NSSize)areaSize withString:(Nsstring *)stringToSize usingFont:(Nsstring *)Fontname;{ NSFont * displayFont = nil; NSSize stringSize = NSZeroSize; NSMutableDictionary * FontAttributes = [[NSMutableDictionary alloc] init]; if (areaSize.wIDth == 0.0 || areaSize.height == 0.0) { return 0.0; } NSUInteger FontLoop = 0; for (FontLoop = 1; FontLoop <= kMaxFontSize; FontLoop++) { displayFont = [[NSFontManager sharedFontManager] convertWeight:YES ofFont:[NSFont FontWithname:Fontname size:FontLoop]]; [FontAttributes setobject:displayFont forKey:NSFontAttributename]; stringSize = [stringToSize sizeWithAttributes:FontAttributes]; if (stringSize.wIDth > areaSize.wIDth) break; if (stringSize.height > areaSize.height) break; } [FontAttributes release],FontAttributes = nil; return (CGfloat)FontLoop - 1.0;} 总结 以上是内存溢出为你收集整理的cocoa – 如何在文本单元格中设置字体大小,以便字符串填充单元格的rect?全部内容,希望文章能够帮你解决cocoa – 如何在文本单元格中设置字体大小,以便字符串填充单元格的rect?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)