簡體   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