如何才能使文本比例符合我给出的界限?
解决方法
我过去做过类似的事.
-(void)calcFontSizeToFitRect:(NSRect)r { float targetWidth = r.size.width - xMargin; float targetHeight = r.size.height - yMargin; // the strategy is to start with a small font size and go larger until I'm larger than one of the target sizes int i; for (i=minFontSize; i<maxFontSize; i++) { NSDictionary* attrs = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:currentFontName size:i],NSFontAttributeName,nil]; NSSize strSize = [string sizeWithAttributes:attrs]; [attrs release]; if (strSize.width > targetWidth || strSize.height > targetHeight) break; } [self setCurrentFontSize:(i-1)]; }
字符串变量是您想要调整大小的文本. xMargin和yMargin变量用于您想要的间距. minFontSize和maxFontSize变量限制了您想要的小或大.