get font-weight of an uitextview

北城以北 提交于 2021-02-08 08:35:17

问题


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]];
  }
}

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.


回答1:


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.

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!");

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"....

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




回答2:


I have figured out how to get the font weights, you have to spelunk down to Core Text:

        let ctFont = font as CTFont
        let traits = CTFontCopyTraits(ctFont)
        let dict = traits as Dictionary

        if let weightNumber = dict[kCTFontWeightTrait] as? NSNumber {
            print(weightNumber.doubleValue)
        }

Enjoy!




回答3:


But my problem is, that not every textView uses the systemFont in normal weight, some of them are in bold weight.

If you want to use Bold System Font then you can simply use

[UIFont boldSystemFontOfSize:15.0]; 

However, I am still thinking of that special case in which you need to use font-weight.

Update :

There is nothing in the UIFont Class using which you can get font-weight directly. You can take a look at UIFont Class Reference.

Only thing that you can do is to get the font-name and try to find out the "bold" sub-string in the font name. If any match found that means font-weight of that specific font is "bold".

But, still this is not the most efficient method.




回答4:


You can get a UIFontDescriptor for a font using the fontDescriptor method. Then you get the fontAttributes dictionary of the UIFontDescriptor, get the UIFontDescriptorTraitsAttribute from the dictionary which returns another dictionary, then read the UIFontWeightTrait from that dictionary. Not tested.

Now it's tested: Doesn't work. fontAttributes always returns a dictionary with two keys for font name and font size, and that's it. I suppose "this doesn't work" is also an answer when something should work according to the documentation...

You can try symbolicTraits, but that's not useful either: It returns "bold" only if the whole font family is bold.



来源:https://stackoverflow.com/questions/18331388/get-font-weight-of-an-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!