Skip to content

Instantly share code, notes, and snippets.

@dedene
Forked from rudyryk/TextMeter.cs
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedene/638221936093cc8f9264 to your computer and use it in GitHub Desktop.
Save dedene/638221936093cc8f9264 to your computer and use it in GitHub Desktop.
//
// TextMeter.cs
// Created by Alexey Kinev on 11 Feb 2015.
//
// Licensed under The MIT License (MIT)
// http://opensource.org/licenses/MIT
//
// Copyright (c) 2015 Alexey Kinev <alexey.rudy@gmail.com>
//
// Usage example:
//
// var label = new Label {
// Text = "La-la-la-laaaaaaaaaaaaaaaaa! And even more La-la-la-laaaaa!",
// FontSize = 16.0,
// };
// label.FontFamily = Device.OnPlatform (
// iOS: "HelveticaNeue"
// Android: "Roboto"
// );
// var labelHeight = TextMeter.MeasureTextSize(
// label.Text, 200, label.FontSize, label.FontFamily).Height;
//
using System;
using Xamarin.Forms;
#if __IOS__
using System.Drawing;
using Foundation;
using UIKit;
namespace ZeroFiveBit.Forms.Utils
{
public static class TextMeterImplementation
{
public static Xamarin.Forms.Size MeasureTextSize(string text, double width,
double fontSize, string fontName = null)
{
var nsText = new NSString(text);
var boundSize = new SizeF((float)width, float.MaxValue);
var options = NSStringDrawingOptions.UsesFontLeading |
NSStringDrawingOptions.UsesLineFragmentOrigin;
if (fontName == null)
{
fontName = "HelveticaNeue";
}
var attributes = new UIStringAttributes {
Font = UIFont.FromName(fontName, (float)fontSize)
};
var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;
return new Xamarin.Forms.Size((double)sizeF.Width, (double)sizeF.Height);
}
}
}
#endif
#if __ANDROID__
using Android.Widget;
using Android.Util;
using Android.Views;
using Android.Graphics;
namespace ZeroFiveBit.Forms.Utils
{
public static class TextMeterImplementation
{
private static Typeface textTypeface;
public static Xamarin.Forms.Size MeasureTextSize(string text, double width,
double fontSize, string fontName = null)
{
var textView = new TextView(global::Android.App.Application.Context);
textView.Typeface = GetTypeface(fontName);
textView.SetText(text, TextView.BufferType.Normal);
textView.SetTextSize(ComplexUnitType.Px, (float)fontSize);
int widthMeasureSpec = Android.Views.View.MeasureSpec.MakeMeasureSpec(
(int)width, MeasureSpecMode.AtMost);
int heightMeasureSpec = Android.Views.View.MeasureSpec.MakeMeasureSpec(
0, MeasureSpecMode.Unspecified);
textView.Measure(widthMeasureSpec, heightMeasureSpec);
return new Xamarin.Forms.Size((double)textView.MeasuredWidth,
(double)textView.MeasuredHeight);
}
private static Typeface GetTypeface(string fontName)
{
if (fontName == null)
{
return Typeface.Default;
}
if (textTypeface == null)
{
textTypeface = Typeface.Create(fontName, TypefaceStyle.Normal);
}
return textTypeface;
}
}
}
#endif
namespace ZeroFiveBit.Forms.Utils
{
public static class TextMeter
{
public static Xamarin.Forms.Size MeasureTextSize(string text, double width,
double fontSize, string fontName = null)
{
return TextMeterImplementation.MeasureTextSize(text, width, fontSize, fontName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment