Get Font Weight in iOS 14

0 votes

In iOS 13 and low I got Font.Weight using this extension for UIFont

var weight: UIFont.Weight {
    guard let traits = fontDescriptor.object(forKey: .traits) as? [UIFontDescriptor.TraitKey: Any], let weightValue = traits[.weight] as? CGFloat else { return .regular }
    let weight = UIFont.Weight(rawValue: weightValue)
    return weight
}

But since iOS 14 weightValue is wrong. For example:

let font = UIFont(name: "AvenirNext-Bold", size: 21)
print(font?.weight.rawValue)
print(font?.weight == .bold)

iOS 14 - 0.40000000000000002 false

iOS 13 - 0.40000000596046448 true

Anybody faced with it?


 

Nov 22, 2022 in Mobile Development by gaurav
• 23,260 points
691 views

1 answer to this question.

0 votes

This is not a bug. It is in fact expected to be inaccurate after a high number of decimal places (16 in this case). Floats are calculated via the IEEE 754 standard for Single-Precision Floating-Point arithmetic. The original standard was only accurate up to 7 decimal places for integers between 4 and 6, but this has iteratively improved since the original, for example in 2019 and 2020 (see here for details).

What you see is probably the effect of conditional casting. The conversion from Float to CGFloat loses precision.

answered Dec 15, 2022 by gaurav
• 23,260 points

Related Questions In Mobile Development

0 votes
0 answers
0 votes
0 answers

How to get original image when we zoom in and zoom out the image in ios

Hi i am beginner in ios and ...READ MORE

Nov 17, 2022 in Mobile Development by gaurav
• 23,260 points
408 views
0 votes
0 answers

Accurate timing in iOS

I am looking at the 'Metronome' sample ...READ MORE

Nov 8, 2022 in Mobile Development by gaurav
• 23,260 points
207 views
0 votes
0 answers

Swift - Find antilog for 4 digit Integer in iOS

I need to find an antilog for 5995. ...READ MORE

Nov 8, 2022 in Mobile Development by gaurav
• 23,260 points
757 views
0 votes
1 answer

How to build a font generator in iOS Swift and allowing pasting to clipboard

On your iPhone, you cannot copy and ...READ MORE

answered Sep 22, 2022 in IOS by Rahul
• 9,670 points
590 views
0 votes
0 answers

How to build a font generator in iOS Swift and allowing pasting to clipboard

I'm now building a listview to display ...READ MORE

Sep 23, 2022 in Others by gaurav
• 23,260 points
312 views
0 votes
1 answer

iOS 13 Custom Fonts download and installation

Step 1: From the Home screen, tap ...READ MORE

answered Dec 15, 2022 in Mobile Development by gaurav
• 23,260 points
2,229 views
0 votes
1 answer

Any rate limiter for kitura

Try using KituraLimiter as a rate limiting ...READ MORE

answered Jun 4, 2018 in DevOps Tools by DareDev
• 6,890 points
484 views
0 votes
1 answer

How to get the user's country calling code in iOS?

Import Statement : #import<CoreTelephony/CTCarrier.h> #import <CoreTelephony/CTTelephonyNetworkInfo.h> you can get country ...READ MORE

answered Dec 15, 2022 in Mobile Development by gaurav
• 23,260 points
733 views
0 votes
1 answer

How to create custom Document Icon in iOS 14+ apps

Tap Add to Home Screen and select ...READ MORE

answered Dec 15, 2022 in Mobile Development by gaurav
• 23,260 points
414 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP