UILabel斜体字体裁剪

阿玛·库洛(Amar Kulo)

我正在尝试解决此问题,因为我正在处理的应用程序中有很多字体,因此当用户更改字体时,标签的大小会动态计算。

我的问题是,如果字体像下面的波纹管一样,UILabel会在最后被剪切:

UILabel剪辑示例

到目前为止,这是我尝试过的:

  • 借助CoreText和 CGSize CTFramesetterSuggestFrameSizeWithConstraints ( CTFramesetterRef framesetter, CFRange stringRange, CFDictionaryRef frameAttributes, CGSize constraints, CFRange *fitRange );
  • 借助NSAttributedString和 - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context
  • 借助NSString和 - (CGSize)sizeWithAttributes:(NSDictionary<NSString *,id> *)attrs
  • 使用临时UITextView和sizeThatFits´ andfitToSize`

由于应用程序中有很多字体,因此我需要动态设置标签的宽度,因此UILabel的子类化和添加更多的点drawFrameInRect是行不通的。

Here is sample code on Github.

Any help/advice is appreciated.

Amin Negm-Awad

Even I don't have the time to test it, I'm pretty sure that the problem is that the label size is calculated from the advances. The advance is the amount of movement from one character's base point to the next ones. Typically for an italic font the advance can be smaller than the bounds. Therefore adding the advances will cut the end of the layout.

I.e. Baskerville-Italic H:

(lldb) p bounds[0].size
(CGSize) $6 = (width=33.53515625, height=26.484375)
(lldb) p advances[0]
(CGSize) $7 = (width=30, height=0)

So I think that you have to add the difference between advance and bounding box of the last character, if the text is layout in an ideal way (no compression and so on).

To get both values, you have to call some CT functions:

// What you probably have
CTFontRef font = …;
CFStringRef string = …;
CFAAttributedStringRef attrString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes);

// Get the character and glyph
CFIndex count = CFStringGetLength(string); // The length of the string
UniChar character;
CFStringGetCharacters( string, CFMakeRange( count-1, 1), &character );
CGGlyph glyph;
CTStringGetGlyphsForCharacters( font, &character, &glyph,1  );

// Get the advance
CGSize advance;
CTFontGetAdvancesForGlyphs( font, 0, &glyph, &advance, 1);

// Get the bounds
CGRect bounds;
CTFontGetAdvancesForGlyphs( font, 0, &glyph, &bounds, 1);

Typed in Safari.

加法:您在左侧有相同的问题:大写字母L的前进为0(第一个字符),但是边界框的值为负x。因此左侧被修剪。在那里也增加空间。(到目前为止更容易些。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MathJax字体不带斜体

来自分类Dev

如何在gVim中添加斜体字?

来自分类Dev

如何将粗体和斜体字体应用于NSAttributedString?

来自分类Dev

matplotlib中斜体字体的标题不起作用

来自分类Dev

使用@fontface字体加载斜体

来自分类Dev

如何使用斜体Google字体?

来自分类Dev

为什么斜体字体不起作用

来自分类Dev

emacs中的斜体字

来自分类Dev

闪亮应用程序中titlePanel中的斜体字体

来自分类Dev

ScalaTags段落中的简洁粗体/斜体字

来自分类Dev

斜体字体样式不适用于元素的class属性

来自分类Dev

Java是不是合成斜体字体面孔

来自分类Dev

具有标签跨度和斜体字体属性的奇怪行为

来自分类Dev

使用下标定义xlabel或ylabel时如何在python中禁用斜体字体

来自分类Dev

我可以在gnome终端中获取斜体字吗?

来自分类Dev

使用Perl将斜体字发布到Twitter

来自分类Dev

具有电源线支持和斜体字的Inconsolata字体

来自分类Dev

使用Windows窗体在RichTextBox中设置斜体字的示例

来自分类Dev

字体裁剪在底部

来自分类Dev

如何为自定义字体使用粗体,常规和斜体字体样式?

来自分类Dev

我的Ubuntu安装中缺少斜体字体

来自分类Dev

在单词前和斜体字前插入空格

来自分类Dev

在Javafx标签中使斜体字变为斜体

来自分类Dev

st终端:禁用粗体/斜体字体

来自分类Dev

闪亮应用程序中titlePanel中的斜体字体

来自分类Dev

斜体字体样式不适用于元素的class属性

来自分类Dev

如何将嵌套的斜体字体标签与Xpath匹配?

来自分类Dev

如何在byobu中获取斜体字体

来自分类Dev

文本框中斜体字符的底部截断