我的 Xamarin 窗体应用的设计器为标签字段指定了小型大写字母。 关于如何在 Xamarin for Android 中执行此操作有几个讨论,但我找不到适用于 iOS 的任何内容。
我知道我将需要一个自定义渲染器,并且在这里找到了很好的讨论,但是在该文章中将Swift代码转换为Xamarin iOS的C#时遇到了挑战。
这是该代码:
let systemFont = UIFont.systemFont(ofSize: 24.0, weight: UIFontWeightLight)
let smallCapsDesc = systemFont.fontDescriptor.addingAttributes([
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kUpperCaseType,
UIFontFeatureSelectorIdentifierKey: kUpperCaseSmallCapsSelector
]
]
])
let font = UIFont(descriptor: smallCapsDesc, size: systemFont.pointSize)
我知道我会在我的 OnElementChanged 方法中放入一些东西,但 FontDescriptor 没有 addingAttributes 方法或属性集合,因此在添加其他属性时面临挑战。
字体描述符没有 addingAttributes 方法或属性集合
您正在寻找UIFont.FontDescriptor.CreateWithAttributes
方法。
例:
var systemFont = UIFont.SystemFontOfSize(24, UIFontWeight.Light);
var attributes = new UIFontAttributes(
new UIFontFeature(CTFontFeatureUpperCase.Selector.UpperCaseSmallCaps),
new UIFontFeature(CTFontFeatureLowerCase.Selector.LowerCaseSmallCaps)
);
var smallCapsDesc = systemFont.FontDescriptor.CreateWithAttributes(attributes);
var font = UIFont.FromDescriptor(smallCapsDesc, systemFont.PointSize);