text_utils_ios.mm 821 B

123456789101112131415161718192021222324252627
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gfx/text_utils.h"
  5. #import <UIKit/UIKit.h>
  6. #include <cmath>
  7. #include "base/strings/sys_string_conversions.h"
  8. #include "ui/gfx/font_list.h"
  9. namespace gfx {
  10. int GetStringWidth(const std::u16string& text, const FontList& font_list) {
  11. return std::ceil(GetStringWidthF(text, font_list));
  12. }
  13. float GetStringWidthF(const std::u16string& text, const FontList& font_list) {
  14. NSString* ns_text = base::SysUTF16ToNSString(text);
  15. NativeFont native_font = font_list.GetPrimaryFont().GetNativeFont();
  16. NSDictionary* attributes = @{NSFontAttributeName : native_font};
  17. return [ns_text sizeWithAttributes:attributes].width;
  18. }
  19. } // namespace gfx