繁体   English   中英

如何在Xamarin.Forms的PCL中获取字符串的长度?

[英]How to get the length of a string into PCL of Xamarin.Forms?

有没有办法在PCL项目中找到字符串的长度? 我可以通过使用Application类来获取设备页面的宽度和高度,但是我不知道如何获取字符串的尺寸。

我想知道的PS,用户的设备能否显示全文。

需要针对每个平台自行完成此操作,您需要创建一个PCL接口,如下所示:

public interface CalculateTextWidth
{
    double calculateWidth (string text);
}

然后例如在android上,代码将如下所示:``public class CalculateTextWidth_Android:CalculateTextWidth {public CalculateTextWidth_Android(){}

    public double calculateWidth (string text)
    {
        Rect bounds = new Rect();
        TextView textView = new TextView(Forms.Context);
        textView.Paint.GetTextBounds(text, 0, text.Length, bounds);
        var length = bounds.Width();           
        return length / Resources.System.DisplayMetrics.ScaledDensity;
    }

有关更多信息和其他平台实现,请参见此链接,以了解如何将字符串的长度转换为xamarin的pcl形式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM