如何在Xamarin iOS中使UILabel垂直对齐

时间:2018-09-20 03:53:06

标签: c# xamarin.ios uilabel

我想创建一个弹出窗口。为了进行开发,我使用的是UIView并在标签和图像内使用。我想为视图内的标签提供行间距或垂直文本对齐方式。

这是我的代码

customPinView.Frame = new CGRect(0, 0, 300, 110);
_view = new UIView(new CGRect(0, 0, 300, 100));
_view.BackgroundColor = UIColor.White;
string[] _prjctArray = marker.Title.Split(':');
string[] _prjtLocation = marker.Snippet.Split(':');

UIImageView _img = new UIImageView(new CGRect(0.2, 0, 50, 100));

if (_prjtLocation.Count() >= 4)
{
    string _imgUrl = _prjtLocation[2] + ":" + _prjtLocation[3];
    var _url = new NSUrl(_imgUrl);
    var _data = NSData.FromUrl(_url);
    _img = new UIImageView(UIImage.LoadFromData(_data));
}
else
{
    _img = new UIImageView(UIImage.FromFile("loading.png"));
}

UITextView _prjectName = new UITextView(new CGRect(_img.Frame.GetMaxX() + 5, 0.2, 200, 30));
_prjectName.Text = _prjctArray[0].Length > 15 ? _prjctArray[0].Remove(12) + "..." : _prjctArray[0];

if (_prjectName.Text.Length <= 10)
{
    _prjectName.TextAlignment = UITextAlignment.Justified;
}
else
{
    _prjectName.TextAlignment = UITextAlignment.Left;
}

_prjectName.TextColor = UIColor.FromRGB(31, 117, 209);
_prjectName.Font = _prjectName.Font.WithSize(20);

UITextView _cityName = new UITextView(new CGRect(_img.Frame.GetMaxX() + 5, _prjectName.Frame.GetMaxY(), 150, 25));
_cityName.Text = _prjtLocation[0];
_cityName.TextColor = UIColor.Gray;
_cityName.TextAlignment = UITextAlignment.Center;
_cityName.Font = _cityName.Font.WithSize(15);

UITextView _streetAddress = new UITextView(new CGRect(_img.Frame.GetMaxX() + 5, _cityName.Frame.GetMaxY() - 1, 150, 20));
_streetAddress.Text = _prjtLocation[1];
_streetAddress.TextColor = UIColor.Gray;
_streetAddress.TextAlignment = UITextAlignment.Center;

UITextView _units = new UITextView(new CGRect(_img.Frame.GetMaxX() + 5, _streetAddress.Frame.GetMaxY(), 150, 20));
_units.Text = "Units: " + _prjctArray[2];
_units.TextColor = UIColor.Gray;
_units.TextAlignment = UITextAlignment.Center;
_view.AddSubview(_img);
_view.AddSubview(_prjectName);
_view.AddSubview(_streetAddress);
_view.AddSubview(_cityName);
_view.AddSubview(_units);
_dwnImage = new UIImageView(new CGRect(_view.Frame.GetMidX() - 4, _view.Frame.GetMidY() + 23, 20, 50));
_dwnImage.Image = UIImage.FromFile("dwnArrow.png");

代码的输出看起来像这样

enter image description here

我想要一个这样的窗口

enter image description here

0 个答案:

没有答案