#define kMax@R_419_6837@Size 10000- (CGfloat)@R_419_6837@SizeforAreaSize:(NSSize)areaSize withString:(Nsstring *)stringToSize using@R_419_6837@:(Nsstring *)@R_419_6837@name;{ NS@R_419_6837@ * display@R_419_6837@ = nil; NSSize stringSize = NSZeroSize; NSMutableDictionary * @R_419_6837@Attributes = [[NSMutableDictionary alloc] init]; if (areaSize.wIDth == 0.0 && areaSize.height == 0.0) return 0.0; NSUInteger @R_419_6837@Loop = 0; for (@R_419_6837@Loop = 1; @R_419_6837@Loop <= kMax@R_419_6837@Size; @R_419_6837@Loop++) { display@R_419_6837@ = [[NS@R_419_6837@Manager shared@R_419_6837@Manager] convertWeight:YES of@R_419_6837@:[NS@R_419_6837@ @R_419_6837@Withname:@R_419_6837@name size:@R_419_6837@Loop]]; [@R_419_6837@Attributes setobject:display@R_419_6837@ forKey:NS@R_419_6837@Attributename]; stringSize = [stringToSize sizeWithAttributes:@R_419_6837@Attributes]; if (stringSize.wIDth > areaSize.wIDth) break; if (stringSize.height > areaSize.height) break; } [@R_419_6837@Attributes release],@R_419_6837@Attributes = nil; return (CGfloat)@R_419_6837@Loop - 1.0;}解决方法 选择任何字体大小并测量该大小的文本.将每个尺寸(宽度和高度)除以目标矩形的相同尺寸,然后将字体大小除以较大的系数.
请注意,文本将在一行上测量,因为它没有最大宽度可以换行.对于长行/字符串,这可能会导致字体大小无用.对于文本字段,您应该只强制执行最小大小(例如小系统字体大小),并设置字段的截断行为.如果您打算包装文本,则需要使用带有边界矩形或大小的内容来测量它.
提问者的代码大致基于这个想法:
-(float)scaletoAspectFit:(CGSize)source into:(CGSize)into padding:(float)padding{ return MIN((into.wIDth-padding) / source.wIDth,(into.height-padding) / source.height);}-(NS@R_419_6837@*)@R_419_6837@SizedForAreaSize:(NSSize)size withString:(Nsstring*)string using@R_419_6837@:(NS@R_419_6837@*)@R_419_6837@;{ NS@R_419_6837@* sample@R_419_6837@ = [NS@R_419_6837@ @R_419_6837@WithDescriptor:@R_419_6837@.@R_419_6837@Descriptor size:12.];//use standard size to prevent error accrual CGSize sampleSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:sample@R_419_6837@,NS@R_419_6837@Attributename,nil]]; float scale = [self scaletoAspectFit:sampleSize into:size padding:10]; return [NS@R_419_6837@ @R_419_6837@WithDescriptor:@R_419_6837@.@R_419_6837@Descriptor size:scale * sample@R_419_6837@.pointSize];}-(voID)windowDIDResize:(NSNotification*)notification{ text.@R_419_6837@ = [self @R_419_6837@SizedForAreaSize:text.frame.size withString:text.stringValue using@R_419_6837@:text.@R_419_6837@];}总结
以上是内存溢出为你收集整理的cocoa – 如何计算(不搜索)字体大小以适合矩形?全部内容,希望文章能够帮你解决cocoa – 如何计算(不搜索)字体大小以适合矩形?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)