如何更改地图标题中的字体大小 iOS Xamarin (C#)
How to change size of font in Map title iOS Xamarin (C#)
我正在为 iOS (Xamarin C#)
编写应用程序
我有一些带有标题的地图,但字体太大。我怎样才能让它变小?
这是我的地图代码:
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# 中,事情变得比它们应该的更复杂(恕我直言),因为您无法在 MKPointAnnotation
中覆盖系统字体,因为您无法访问子视图构造(或层次结构) ) 也不会在系统字体调用上做一些临时的 ObjC 魔法。
- 如果有人知道如何请告诉我;-)
我通常将所有这些隐藏在子类的 MKMapView 中,该子类使用完全自定义绘制的注释视图而不是 MKPointAnnotation
,但这种方式最容易遵循。
因此,在开始添加注释之前,我将自定义委托分配给 MKMapView 的 GetViewForAnnotation
(MKMapViewDelegate
)属性。
示例Setup/Call:
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
s 上的字体分配:
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 点):
我正在为 iOS (Xamarin C#)
编写应用程序我有一些带有标题的地图,但字体太大。我怎样才能让它变小?
这是我的地图代码:
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# 中,事情变得比它们应该的更复杂(恕我直言),因为您无法在 MKPointAnnotation
中覆盖系统字体,因为您无法访问子视图构造(或层次结构) ) 也不会在系统字体调用上做一些临时的 ObjC 魔法。
- 如果有人知道如何请告诉我;-)
我通常将所有这些隐藏在子类的 MKMapView 中,该子类使用完全自定义绘制的注释视图而不是 MKPointAnnotation
,但这种方式最容易遵循。
因此,在开始添加注释之前,我将自定义委托分配给 MKMapView 的 GetViewForAnnotation
(MKMapViewDelegate
)属性。
示例Setup/Call:
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
s 上的字体分配:
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 点):