简体   繁体   English

自定义字体在可可中放错位置

[英]Custom font misplaced in Cocoa

When using non-standard fonts in Cocoa, they are sometimes misplaced compared to standard system fonts. 在Cocoa中使用非标准字体时,与标准系统字体相比,它们有时会放错位置。 I've created a sample OS X Cocoa Application which illustrates the issue: http://www.fileconvoy.com/dfl.php?id=g2bff79a0dcf64cc4999493513da42fd8dbb9294e1 . 我创建了一个示例OS X Cocoa应用程序来说明问题: http : //www.fileconvoy.com/dfl.php?id=g2bff79a0dcf64cc4999493513da42fd8dbb9294e1

It looks like this: 看起来像这样:

自定义字体放到标签和按钮内

The label and button on left is the default system font, whereas on the right, the font is changed to Al Bayan (shipped with OS X). 左侧的标签和按钮是默认的系统字体,而右侧的字体更改为Al Bayan (OS X附带)。

I'm developing an application, where the font has to be changed dynamically. 我正在开发一个应用程序,该字体必须动态更改。 If the user changes the font to one with similar behavior like this, I need some technique to change the margin/padding inside the controls, in this case like: 如果用户将字体更改为具有类似行为的字体,则需要某种技术来更改控件内部的边距/填充,在这种情况下,例如:

  • Button: +5px top margin/padding 按钮:+ 5px上边距/填充
  • Label: -5px top margin/padding 标签:-5px上边距/填充

How can this be done? 如何才能做到这一点?

This is especially worse with "Helvetica Neue" which is kinda standard font (in iOS) and likely to play a bigger role in future OS X versions. 对于“ Helvetica Neue”来说尤其糟糕,后者是一种标准字体(在iOS中),并且可能在将来的OS X版本中发挥更大的作用。 I'm not aware of a way to adjust individual controls other than changing the font, but I found a way to render text correctly with own computations. 除了更改字体,我不知道一种调整单个控件的方法,但是我找到了一种使用自己的计算正确呈现文本的方法。 Key is here not to use the string height (eg for centering or placing the string), but to take the ascender and xHeight into account. 此处的关键不是使用字符串的高度(例如,使字符串居中或放置),而是要考虑到上升器和xHeight。

The following code draws the title of an NSTextFieldCell vertically centered, regardless of the font used: 以下代码绘制一个垂直居中的NSTextFieldCell的标题,而不管使用什么字体:

- (NSRect)titleRectForBounds: (NSRect)theRect
{
    NSRect titleFrame = [super titleRectForBounds: theRect];
    CGRect rect = [self.attributedStringValue boundingRectWithSize: NSMakeSize(FLT_MAX, NSHeight(titleFrame))
                                                           options: 0];
    titleFrame.origin.y -= NSHeight(rect) - floor(self.font.ascender);
    titleFrame.origin.y += floor((titleFrame.size.height - self.font.xHeight) / 2);
    return titleFrame;
}

- (void)drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView
{
    NSRect titleRect = [self titleRectForBounds: cellFrame];
    [self.attributedStringValue drawInRect: titleRect];
}

The cell is part of an NSOutlineView and hence flipped. 该单元格是NSOutlineView的一部分,因此被翻转了。 For non-flipped content the solution is probably a bit simpler (haven't tested that). 对于非翻转内容,解决方案可能会更简单(尚未测试)。

Use setAttributedTitle: of the NSButtonCell class. 使用NSButtonCell类的setAttributedTitle:。 Supply it an NSAttributedString See the Attributed String Programming Guide for guidance. 向其提供NSAttributedString,请参见《属性字符串编程指南》以获取指导。 The font will be part of an NSMutableParagraphStyle defining the text styling. 该字体将成为NSMutableParagraphStyle的一部分,该样式定义文本样式。 The standard buttons are doing all of this with predefined styling. 标准按钮通过预定义的样式来完成所有这些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM