繁体   English   中英

如何在地图标题iOS Xamarin(C#)中更改字体大小

[英]How to change size of font in Map title iOS Xamarin (C#)

我正在为iOS(Xamarin C#)编写应用程序

我有一些标题的地图,但是它的字体太大。 如何缩小尺寸?

这是我的Map代码:

MKMapView map = new MKMapView (UIScreen.MainScreen.Bounds);

        map.AddAnnotations (new MKPointAnnotation (){

            Title="МУРАКАМИ В ТРЦ «ОКЕАН ПЛАЗА»",
            Subtitle ="Пн - Пт 10.00 -00.00" +
                " Cб.- Вс 10.00 - 00.00",

            Coordinate = new CLLocationCoordinate2D (50.412300, 30.522756)
        });

和截图

在此处输入图片说明

在Swift或C#中,事情变得比应有的更为复杂(IMHO),因为您无法覆盖MKPointAnnotation的系统字体,因为您无法访问子视图构造(或层次结构),也无法在系统字体调用上执行一些临时性的ObjC魔术。

  • 如果有人不知道如何告诉我;-)

我通常将所有这些隐藏在使用完全自定义绘制的Annotation视图而不是MKPointAnnotation的子类MKMapView中,但是这种方式最容易遵循。

因此,在开始添加注释之前,我将自定义委托分配给MKMapViewDelegateGetViewForAnnotationMKMapViewDelegate )属性。

设置/呼叫示例:

mapView.GetViewForAnnotation = myViewForAnnotation;
mapView.AddAnnotations (new MKPointAnnotation (){
    Title="МУРАКАМИ В ТРЦ «ОКЕАН ПЛАЗА»",
    Subtitle ="Пн - Пт 10.00 -00.00" +
    " Cб.- Вс 10.00 - 00.00",
    Coordinate = new CLLocationCoordinate2D (50.412300, 30.522756)
});

自定义GetViewForAnnotation(MKMapViewDelegate):

在这里,您可以为需要创建的任何新MKPointAnnotation对象分配自定义字体,但实际上我们正在创建自定义MKAnnotationView

public MyAnnotationView myViewForAnnotation(MKMapView mapView, IMKAnnotation id)
{
    if (id is MKPointAnnotation) {
        MyAnnotationView view = (MyAnnotationView)mapView.DequeueReusableAnnotation ("myCustomView");
        if (view == null) {
            view = new MyAnnotationView (id, "myCustomView", UIFont.FromName ("Chalkboard SE", 16f));
        } else {
            view.Annotation = id;
        }
        view.Selected = true;
        return view;
    }
    return null;
}

自定义MyAnnotationView(MKAnnotationView子类):

这将存储通过构造函数传递的自定义字体,并处理在其子视图中存在的任何UILabel上分配的字体:

using System;
using MapKit;
using UIKit;

namespace mkmapview
{
    public class MyAnnotationView : MKAnnotationView // or MKPointAnnotation
    {
        UIFont _font;
        public MyAnnotationView (IMKAnnotation annotation, string reuseIdentifier, UIFont font) : base (annotation, reuseIdentifier)
        {
            _font = font;
            CanShowCallout = true;
            Image = UIImage.FromFile ("Images/MapPin.png");
        }

        void searchViewHierarchy (UIView currentView)
        {
            // short-circuit
            if (currentView.Subviews == null || currentView.Subviews.Length == 0) {
                return;
            }
            foreach (UIView subView in currentView.Subviews) {
                if (subView is UILabel) {
                    (subView as UILabel).Font = _font;
                } else {
                    searchViewHierarchy (subView);
                }
            }
        }

        public override void LayoutSubviews ()
        {
            if (!Selected)
                return;
            base.LayoutSubviews ();
            foreach (UIView view in Subviews) {
                Console.WriteLine (view);
                searchViewHierarchy (view);
            }
        }
    }
}

系统字体/默认大小:

在此处输入图片说明

系统字体/ 10分

在此处输入图片说明

自定义字体(黑板/ 10点):

在此处输入图片说明

暂无
暂无

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

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