获取 uitextview 的字体粗细 [英] get font-weight of an uitextview

查看:88
本文介绍了获取 uitextview 的字体粗细的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一些 UITextViews 中调整字体大小.这适用于插座集合和此代码:

I want to resize the font-size in some UITextViews. That works fine with an outlet collection and this code:

for (UITextView *view in self.viewsToResize) {
  if ([view respondsToSelector:@selector(setFont:)]) {
     [view setFont:[UIFont systemFontOfSize:view.font.pointSize + 5]];
  }
}

但我的问题是,并非每个 textView 都使用正常粗细的 systemFont,其中一些使用粗体.是否有可能获得字体粗细?使用调试区域中的 po view.font 我可以看到我需要的一切:

But my problem is, that not every textView uses the systemFont in normal weight, some of them are in bold weight. Is it possible to get the font-weight? With a po view.font in the debug area I can see everything I need:

$11 = 0x0c596ea0 <UICFFont: 0xc596ea0> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12px

但是我如何访问字体粗细?

But how can I access the font-weight?

为粗体视图使用第二个插座集合可以解决我的问题.但我想知道我没有发现只能获得字体粗细.

Using a second outlet collection for the bold views could solve my problem. But I'm wondering that I found nothing to get only the font-weight.

推荐答案

UIFont 没有粗体/斜体/...属性,因此您只能依赖字体名称.如果您不知道将使用哪些字体,这将是一个问题.

UIFont does not have a bold/italic/... property, so you will have to rely on the font name only. This will be a problem if you don't know which fonts will be used.

如果您知道将使用例如.只有 Helvetica 你可以试试这个:

In the case you know that you will use eg. only Helvetica you can try this:

UIFont *font = textview.font;
if([font.fontName isEqualToString:@"Helvetica-Bold"])
    NSLog(@"It's Bold!");

或者,您可以在 font.fontName 中搜索单词粗体"/中等"/浅色"等,但这并不能保证您会从每种可用字体中获得某些内容:

Alternatively you can search font.fontName for the word "bold"/"medium"/"light" etc., but that's not a guarantee you will get something from every available font:

if ([font.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location == NSNotFound) {
  NSLog(@"font is not bold");
} else {
  NSLog(@"font is bold!");
}
// if font.fontName contains "medium"....
// if font.fontName contains "italic"....

检查 http://iosfonts.com/ 以获得可用的字体名称.

Check http://iosfonts.com/ for the available font names.

这篇关于获取 uitextview 的字体粗细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆