我无法在 Xamarin Studio 上使用 C# 更改 CollectionViewCell 的 UILabel 字体
I Cannot Change CollectionViewCell's UILabel Font with C# on Xamarin Studio
我在 ViewController 上有一个 CollectionView,它是 RootTabController 的一个选项卡。此 CollectionView 的 Cell 设计为 .xib:
西伯 UI:
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="GridBoxCell" id="cZE-iV-UFb" customClass="GridBoxViewCell">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</view>
<color key="backgroundColor" red="0.20000000000000001" green="0.031372549019607843" blue="0.38039215686274508" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="4" firstAttribute="top" secondItem="cZE-iV-UFb" secondAttribute="top" id="10"/>
<constraint firstItem="4" firstAttribute="leading" secondItem="cZE-iV-UFb" secondAttribute="leading" id="11"/>
<constraint firstAttribute="bottom" secondItem="4" secondAttribute="bottom" id="12"/>
<constraint firstAttribute="trailing" secondItem="4" secondAttribute="trailing" id="13"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
<real key="value" value="5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<outlet property="Value_Label" destination="4" id="name-outlet-4"/>
</connections>
</collectionViewCell>
</objects>
我正在尝试通过基于设备屏幕大小的代码在构造阶段更改 UıLabel 的字体,同时在添加到 CollectionView 之前加载 Xib:
Xib代码:
protected GridBoxViewCell(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
var selectback_view = new UIView(); // Selectedbackground UIview.
selectback_view.BackgroundColor = UIColor.FromRGB(117, 24, 73); // pink Color
SelectedBackgroundView = selectback_view; // Selected background assigned.
if (UIDevice.CurrentDevice.Model.Contains("iPad")) // If a iPad
{
Value_Label.Font = UIFont.FromName(".SFUIText", 20f); // Increse font width.
}
else // If a iPhone or iPod Touch
{
if (UIScreen.MainScreen.Bounds.Width == 414 && UIScreen.MainScreen.Bounds.Height == 736) // a iPhone Plus 5.5 in point
{
Value_Label.Font = UIFont.FromName(".SFUIText", 16f); // Increse font width.
}
if (UIScreen.MainScreen.Bounds.Width == 375 && UIScreen.MainScreen.Bounds.Height == 667) // a iPhone 4.7 in point
{
Value_Label.Font = UIFont.FromName(".SFUIText", 14f); // Increse font width.
}
}
}
但是,这一行给出了 NullReferenceException:
Value_Label.Font = UIFont.FromName(".SFUIText", 14f); // Increse font width.
位于键盘顶部的 UIBarButton "Done" 将在 UITextBox 上输入的值添加到基于 LinkedList 的 GridViewDataSource。 CollectionView的刷新数据显示方块。
一开始是可以的,现在不行了。我无法弄清楚为什么它不起作用 now.Also 请注意,xib 运行没有问题 4" iPhones like SE 因为我没有为它编写任何约束,它使用默认字体。如果你我会很高兴可以帮助我。谢谢。
您应该覆盖 awakeFromNib
方法并在该方法中分配字体。
我在 ViewController 上有一个 CollectionView,它是 RootTabController 的一个选项卡。此 CollectionView 的 Cell 设计为 .xib:
西伯 UI:
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="GridBoxCell" id="cZE-iV-UFb" customClass="GridBoxViewCell">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</view>
<color key="backgroundColor" red="0.20000000000000001" green="0.031372549019607843" blue="0.38039215686274508" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="4" firstAttribute="top" secondItem="cZE-iV-UFb" secondAttribute="top" id="10"/>
<constraint firstItem="4" firstAttribute="leading" secondItem="cZE-iV-UFb" secondAttribute="leading" id="11"/>
<constraint firstAttribute="bottom" secondItem="4" secondAttribute="bottom" id="12"/>
<constraint firstAttribute="trailing" secondItem="4" secondAttribute="trailing" id="13"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
<real key="value" value="5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<outlet property="Value_Label" destination="4" id="name-outlet-4"/>
</connections>
</collectionViewCell>
</objects>
我正在尝试通过基于设备屏幕大小的代码在构造阶段更改 UıLabel 的字体,同时在添加到 CollectionView 之前加载 Xib:
Xib代码:
protected GridBoxViewCell(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
var selectback_view = new UIView(); // Selectedbackground UIview.
selectback_view.BackgroundColor = UIColor.FromRGB(117, 24, 73); // pink Color
SelectedBackgroundView = selectback_view; // Selected background assigned.
if (UIDevice.CurrentDevice.Model.Contains("iPad")) // If a iPad
{
Value_Label.Font = UIFont.FromName(".SFUIText", 20f); // Increse font width.
}
else // If a iPhone or iPod Touch
{
if (UIScreen.MainScreen.Bounds.Width == 414 && UIScreen.MainScreen.Bounds.Height == 736) // a iPhone Plus 5.5 in point
{
Value_Label.Font = UIFont.FromName(".SFUIText", 16f); // Increse font width.
}
if (UIScreen.MainScreen.Bounds.Width == 375 && UIScreen.MainScreen.Bounds.Height == 667) // a iPhone 4.7 in point
{
Value_Label.Font = UIFont.FromName(".SFUIText", 14f); // Increse font width.
}
}
}
但是,这一行给出了 NullReferenceException:
Value_Label.Font = UIFont.FromName(".SFUIText", 14f); // Increse font width.
位于键盘顶部的 UIBarButton "Done" 将在 UITextBox 上输入的值添加到基于 LinkedList 的 GridViewDataSource。 CollectionView的刷新数据显示方块。
一开始是可以的,现在不行了。我无法弄清楚为什么它不起作用 now.Also 请注意,xib 运行没有问题 4" iPhones like SE 因为我没有为它编写任何约束,它使用默认字体。如果你我会很高兴可以帮助我。谢谢。
您应该覆盖 awakeFromNib
方法并在该方法中分配字体。