简体   繁体   English

在 Xamarin Forms iOS 中使用 UIStringAttributes 时,如何获取 UITextView 的行高?

[英]How do I get line heights of UITextView when using UIStringAttributes in Xamarin Forms iOS?

I have a problem with setting UIStringAttributes.Font .我在设置UIStringAttributes.Font时遇到问题。

Before setting it, the system defaults to Helvetica 12pt , with a line height of 13.8.在设置之前,系统默认为Helvetica 12pt ,行高为 13.8。

This line height seems accurate as shown by the image here:此行高似乎准确,如下图所示:

在此处输入图像描述

在此处输入图像描述

However if I set the UIStringAttributes.Font property with Arial 17pt , the line height of 18.99 seems inaccurate as shown here:但是,如果我使用Arial 17pt设置UIStringAttributes.Font属性,则 18.99 的行高似乎不准确,如下所示:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

My code is as follows:我的代码如下:

void SetAttributes(string text)
{
  NSMutableAttributedString astr = new NSMutableAttributedString();
  UIStringAttributes attrs;
  NSAttributedString str;
  CheckBox cb;
  string[] lines;
  string line;

  lines = text.Split("\n", StringSplitOptions.None);
  for (int i = 0; i < lines.Count; i++)
  {
            cb = _checks[i];
            line = lines[i];

    if (i + 1 < _checks.Count)
      line += "\n";

    attrs = new UIStringAttributes();
    attrs.ForegroundColor = cb.Color;

    ////////////////////////////////
    //COMMENTING THIS LINE OUT ALLOWS A CORRECT
    //LINEHEIGHT TO BE GIVEN IN DRAW METHOD
    attrs.Font = Control.Font;
    ////////////////////////////////

    str = new NSAttributedString(line, attrs);

    astr.Append(str);
        }

  Control.AttributedText = astr;
}

public override void Draw(CGRect rect)
{
  double baseline = editor.Padding.Top + Control.Font.Ascender;
  double movingBaseline = baseline + BOTTOM_MARGIN;
  double scrollY = Control.ContentOffset.Y;

  double _lineHeight = Control.Font.LineHeight;

  //get graphics context
  using (CGContext g = UIGraphics.GetCurrentContext())
  {
    for (int i = 0; i < _lines.Count; i++)
    {
        CGPath path = new CGPath();

      //set up drawing attributes
      g.SetLineWidth(0.5f);
      UIColor.Black.SetStroke();

      //add lines to the touch points
      path.AddLines(new CGPoint[] { new CGPoint(0, movingBaseline - scrollY),
                  new CGPoint(300, movingBaseline - scrollY) });

      //add geometry to graphics context and draw it
      g.AddPath(path);

      g.DrawPath(CGPathDrawingMode.Stroke);

      //tying this to the control.baseline hopefully should help
      //to avoid accumulating rounding errors
      movingBaseline = baseline + BOTTOM_MARGIN + (_lineHeight * (double)(i + 1));
    }
  }


  base.Draw(rect);

}

I've tried:我试过了:

  • 1 other font, Consolas -> height still wrong其他 1 种字体, Consolas -> height 仍然错误
  • NSString(text).StringSize -> gives height of 19 which is wrong NSString(text).StringSize -> 给出 19 的高度,这是错误的
  • Setting ParagraphStyle.LineHeight in UIStringAttributes so I can control the height, but unfortunately it is not settable because Xamarin hasn't implemented a setterUIStringAttributes中设置ParagraphStyle.LineHeight以便我可以控制高度,但不幸的是它不可设置,因为 Xamarin 尚未实现设置器

Any ideas?有任何想法吗?

I believe I may have solved it, or at least have a workaround with:我相信我可能已经解决了它,或者至少有一个解决方法:

NSMutableParagraphStyle para = new NSMutableParagraphStyle();

para.LineSpacing = LINESPACING;
attrs.ParagraphStyle = para;

Setting the Font on the attributes was likely adding a default linespacing which I had no visibility of.属性上设置字体可能会添加一个我看不到的默认行距。 If I explicitly set it myself, at least I know what value is being used and can calculate the line height as:如果我自己明确设置它,至少我知道正在使用什么值并且可以将行高计算为:

lineHeight = Control.Font.LineHeight + LINESPACING

This seems to work for now.这似乎暂时有效。 Will report back if it starts misbehaving again.如果它再次开始行为不端,将报告。

暂无
暂无

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

相关问题 如何使用Xamarin.Forms.iOS在渲染器视图中更改UIPickerView文本颜色? - How do I change UIPickerView text color in renderer view using Xamarin.Forms.iOS? 如何在Xamarin Forms for iOS中创建导航栏? - How do I create a Navigation Bar in Xamarin Forms for iOS? 如何使用iOS共享扩展程序使用Xamarin Forms从Photos应用程序检索图像? - How do I use iOS Share Extension to retrieve an image from the Photos App using Xamarin Forms? 在xamarin.ios中添加新行时,如何实现多行/自动调整大小的UITextView,类似于SMS-app或Skype应用 - How to implement Multi-line/AutoResizing UITextView when new line added in xamarin.ios, similar to SMS-app or Skype app 如何在 Xamarin.Forms 中显示可以显示 IOS 和 Android 上阈值的详细弹出窗口和水平线的折线图? - How do I display a Line Graph in Xamarin.Forms that can show detail popups and horizontal lines for thresholds on IOS and Android? 如何在 xamarin.forms 中获取应用程序本身的目录 - How do I get the directory for the app itself in xamarin.forms 如何使用 Xamarin Forms 正确旋转 Label? - How do I rotate a Label correctly using Xamarin Forms? 如何使用 Xamarin Forms Shell 在 iOS 中的 Tabbar 行和图标之间添加空格? - How to add a space between the Tabbar line and the icons in iOS using Xamarin Forms Shell? Xamarin iOS-绑定库时如何包括依赖项? - Xamarin iOS - How do I include dependencies when binding libraries? 如何在 Xamarin 表单中使用 EditableFactory? - How do I use EditableFactory in Xamarin Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM