Naa*_*aff 62
点尺寸定义为1/72英寸.也就是说,从最低下降到最高上升,72点字体大约为1英寸.因此,72pt字体中字形的最大高度约为1英寸.
Apple的iphone技术规格页面声称iPhone 目前的分辨率为每英寸163像素.所以72点是163像素,或每点约2.2639像素.请记住,每个字形的高度和宽度都不同,因此这是一个非常粗略的大小估计.通常,基线之间的距离将略大于字体的磅值,以使文本行不会相互崩溃.
如果您需要精确测量(并且您可能需要),那么您需要使用字体度量信息实际测量字体字形.您可以使用NSString的UIKit添加来执行此操作,这将使您可以在屏幕上呈现时测量特定字符串的大小.
小智 32
要将iPhone4上的字体大小(以磅为单位)与Photoshop中的字体大小(以磅为单位)相匹配,您必须将Photoshop文档设置为144dpi.我已经进行了多次测试,这就是产生1:1结果的分辨率.
脚步:
我已经经历了许多不同的决议,包括上面答案中提到的163dpi,我发现144dpi产生1:1的结果.我还针对原生应用测试了这个,我知道点大小和144dpi也匹配.
我们的图形艺术家在某些设备上非常具体,使用像素大小而不是点大小.下面的函数将返回基于像素大小的字体.它使用强力方法来查找壁橱字体,然后缓存结果,以便下次返回非常快.我总是欣赏有关如何使这些代码更好的评论.我在函数类中使用此函数作为静态类成员,名为utils.您可以轻松粘贴到您正在使用的任何课程中.希望它有所帮助.
/** return a font as close to a pixel size as possible
example:
UIFont *font = [Utils fontWithName:@"HelveticaNeue-Medium" sizeInPixels:33];
@param fontName name of font same as UIFont fontWithName
@param sizeInPixels size in pixels for font
*/
+(UIFont *) fontWithName:(NSString *) fontName sizeInPixels:(CGFloat) pixels {
static NSMutableDictionary *fontDict; // to hold the font dictionary
if ( fontName == nil ) {
// we default to @"HelveticaNeue-Medium" for our default font
fontName = @"HelveticaNeue-Medium";
}
if ( fontDict == nil ) {
fontDict = [ @{} mutableCopy ];
}
// create a key string to see if font has already been created
//
NSString *strFontHash = [NSString stringWithFormat:@"%@-%f", fontName , pixels];
UIFont *fnt = fontDict[strFontHash];
if ( fnt != nil ) {
return fnt; // we have already created this font
}
// lets play around and create a font that falls near the point size needed
CGFloat pointStart = pixels/4;
CGFloat lastHeight = -1;
UIFont * lastFont = [UIFont fontWithName:fontName size:.5];\
NSMutableDictionary * dictAttrs = [ @{ } mutableCopy ];
NSString *fontCompareString = @"Mgj^";
for ( CGFloat pnt = pointStart ; pnt < 1000 ; pnt += .5 ) {
UIFont *font = [UIFont fontWithName:fontName size:pnt];
if ( font == nil ) {
NSLog(@"Unable to create font %@" , fontName );
NSAssert(font == nil, @"font name not found in fontWithName:sizeInPixels" ); // correct the font being past in
}
dictAttrs[NSFontAttributeName] = font;
CGSize cs = [fontCompareString sizeWithAttributes:dictAttrs];
CGFloat fheight = cs.height;
if ( fheight == pixels ) {
// that will be rare but we found it
fontDict[strFontHash] = font;
return font;
}
if ( fheight > pixels ) {
if ( lastFont == nil ) {
fontDict[strFontHash] = font;
return font;
}
// check which one is closer last height or this one
// and return the user
CGFloat fc1 = fabs( fheight - pixels );
CGFloat fc2 = fabs( lastHeight - pixels );
// return the smallest differential
if ( fc1 < fc2 ) {
fontDict[strFontHash] = font;
return font;
} else {
fontDict[strFontHash] = lastFont;
return lastFont;
}
}
lastFont = font;
lastHeight = fheight;
}
NSAssert( false, @"Hopefully should never get here");
return nil;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63819 次 |
| 最近记录: |