wpl*_*g11 8 c# xamarin.ios ios uialertcontroller
UIAlertController自定义字体不起作用.
以下代码是一个函数ShowActionSheetAsync,show ActionSheet.此时,我想改变字体ActionSheet.我尝试了几种方法,但效果不佳.有好的解决方案吗?
public Task<bool> ShowActionSheetAsync()
{
var source = new TaskCompletionSource<bool>();
var alert = new UIAlertController
{
Title = "title"
};
alert.AddAction(UIAlertAction.Create(
"button1",
UIAlertActionStyle.Default,
_ => source.SetResult(true)));
alert.AddAction(UIAlertAction.Create(
"cancel",
UIAlertActionStyle.Cancel,
_ => source.SetResult(false)));
// [Block 1]
var viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
ViewController.PresentViewController(alert, true, delegate
{
// [Block 2]
});
// [Block 3]
return source.Task;
}
Run Code Online (Sandbox Code Playgroud)
首次尝试 以下代码无法正常运行.
当我把代码放在[Block 1]或[Block 2]
当我把代码放在上面 [Block 3]
ActionSheet.从第二次起,它不起作用
UILabel.AppearanceWhenContainedIn(typeof(UIActionSheet)).Font
= UIFont.FromName(StyleResources.MediumFontName, 20);
第二次尝试 以下代码也无法正常工作.
当我把代码放在上面 [Block 2]
当我把代码放在上面 [Block 3]
cancel按钮FindDescendantViews<UILabel>()是一个扩展方法,UIView并返回适当类型的所有子视图.
var labels = alert.View.FindDescendantViews<UILabel>();
foreach (var label in labels)
{
label.Font = UIFont.FromName(StyleResources.MediumFontName, 20);
}
Run Code Online (Sandbox Code Playgroud)
UIAlertController UILabels字体和颜色可以通过 KVC 使用 键设置attributedTitle。使用这个[Block 3]
func changeAlert(alert: UIAlertController, backgroundColor: UIColor, textColor: UIColor, buttonColor: UIColor?) {
let view = alert.view.firstSubview().firstSubview()
view.backgroundColor = backgroundColor
view.layer.cornerRadius = 10.0
// set color to UILabel font
setSubviewLabelsToTextColor(textColor, view: view)
// set font to alert via KVC, otherwise it'll get overwritten
let titleAttributed = NSMutableAttributedString(
string: alert.title!,
attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(17)])
alert.setValue(titleAttributed, forKey: "attributedTitle")
let messageAttributed = NSMutableAttributedString(
string: alert.message!,
attributes: [NSFontAttributeName:UIFont.systemFontOfSize(13)])
alert.setValue(messageAttributed, forKey: "attributedMessage")
// set the buttons to non-blue, if we have buttons
if let buttonColor = buttonColor {
alert.view.tintColor = buttonColor
}
}
func setSubviewLabelsToTextColor(textColor: UIColor, view:UIView) {
for subview in view.subviews {
if let label = subview as? UILabel {
label.textColor = textColor
} else {
setSubviewLabelsToTextColor(textColor, view: subview)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
952 次 |
| 最近记录: |