Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for MeasurementFormatter #2047

Closed
devajith opened this issue Jun 6, 2020 · 3 comments
Closed

Support for MeasurementFormatter #2047

devajith opened this issue Jun 6, 2020 · 3 comments

Comments

@devajith
Copy link

devajith commented Jun 6, 2020

How to use MeasurementFormatter with DecimalRow

let speed = Measurement<UnitSpeed>(value: 111.6,
                                   unit: .kilometersPerHour)

let formatter = MeasurementFormatter()
formatter.string(from: speed)
@devajith
Copy link
Author

devajith commented Jun 7, 2020

By referring 684 did a workaround

class AreaFormatter: NumberFormatter, FormatterProtocol {
        
    override func string(for obj: Any?) -> String? {
        if let obj = obj as? NSNumber {
            let squareMeters = Measurement(value: obj.doubleValue, unit: UnitArea.squareMeters)
            let formatter = MeasurementFormatter()
            return  formatter.string(from: squareMeters)
        }
        return nil
    }
    
    override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, range rangep: UnsafeMutablePointer<NSRange>?) throws {
        guard obj != nil else { return }
        let str = string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
        obj?.pointee = NSNumber(value: (Double(str) ?? 0.0)/Double(pow(10.0, Double(minimumFractionDigits))))
    }
    
    func getNewPosition(forPosition: UITextPosition, inTextInput textInput: UITextInput, oldValue: String?, newValue: String?) -> UITextPosition {
        if let selectedRange = textInput.selectedTextRange {
            if let newPosition = textInput.position(from: selectedRange.start, offset: -" \(UnitArea.squareMeters.symbol)".count) {
                return newPosition;
            }
        }
        return textInput.position(from: forPosition, offset:((newValue?.count ?? 0) - (oldValue?.count ?? 0))) ?? forPosition
    }
    
}
<<< DecimalRow(){
                $0.useFormatterDuringInput = true
                $0.title = "Plot Area"
                $0.value = 344.78
                let formatter = AreaFormatter()
                formatter.minimumFractionDigits = 2;
                $0.formatter = formatter
            }

@mats-claassen
Copy link
Member

Hey @devajith that looks good. Did it work? You can also post those questions to StackOverflow

@devajith
Copy link
Author

Yes it is working as expected. Ok i will post it in StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants