在iPhone上,如何计算给定磅值的字符大小(
)答案 0 :(得分:62)
磅值为defined as 1/72 of an inch。也就是说,从最低下降到最高上升,72点字体大约为1英寸。因此,72pt字体中字形的最大高度约为1英寸。
Apple's iphone tech specs page声称iPhone 目前的分辨率为每英寸163像素。所以72点是163像素,或每点约2.2639像素。请记住,每个字形的高度和宽度都不同,因此这是一个非常粗略的大小估计。通常,baselines之间的距离会略大于字体的磅值,因此文本行不会相互崩溃。
如果您需要精确测量(并且您可能需要),那么您需要使用字体度量信息实际测量字体字形。您可以使用NSString's UIKit additions执行此操作,这样可以在屏幕上呈现时测量特定字符串的大小。
答案 1 :(得分:31)
要将iPhone4上的字体大小(以磅为单位)与Photoshop中的字体大小(以磅为单位)相匹配,您必须将Photoshop文档设置为144dpi。我已经进行了多次测试,这就是产生1:1结果的分辨率。
<强>步骤:强>
我已经经历了许多不同的决议,包括上面答案中提到的163dpi,我发现144dpi会产生1:1的结果。我也在本地应用程序中对此进行了测试,我知道点大小和144dpi也匹配。
答案 2 :(得分:5)
我们的图形艺术家在某些设备上使用像素大小而非点大小非常具体。 下面的函数将返回基于像素大小的字体。 它使用强力方法来查找壁橱字体,然后缓存结果,以便下次返回非常快。我总是欣赏有关如何使这些代码更好的评论。我在函数类中使用此函数作为静态类成员,名为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;
}
答案 3 :(得分:3)
答案 4 :(得分:1)
获取给定字体和大小的像素高度的最简单方法是在NSString上使用boundingRect方法。 (我在这里使用@"Ap"
来确保它包含下行器和上升器。)
- (CGFloat)heightForFont:(UIFont *)font
{
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGRect boundingRect = [@"Ap" boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:context];
return boundingRect.size.height;
}
答案 5 :(得分:0)
由于ppi(每英寸点数)将从显示器更改为显示器,因此无法将点可靠地转换为像素。读一读;
尽管如此,有些人已经将一些参考表和计算器放在一起,可能会让你开始;
http://www.unitconversion.org/typography/postscript-points-to-pixels-x-conversion.html
答案 6 :(得分:0)
我会在我弄清楚的时候添加它们:
带有12pt systemFont的sizeToFit UILabel高15px。