问题描述
UIAlertController自定义字体不起作用。
下面的代码是一个函数 ShowActionSheetAsync
,显示 ActionSheet
。
。在这一点上,我想改变 ActionSheet
的字体。我曾尝试多种方法,但它并不能很好地工作。有没有好的解决办法?
公共任务<布尔> ShowActionSheetAsync()
{
无功源=新TaskCompletionSource<布尔>();
变种警报=新UIAlertController
{
标题=标题为
};
alert.AddAction(UIAlertAction.Create(
按钮1,
UIAlertActionStyle.Default,
_ => source.SetResult(真)));
alert.AddAction(UIAlertAction.Create(
取消,
UIAlertActionStyle.Cancel,
_ => source.SetResult(假)));
// [块1]
变种的viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
ViewController.PresentViewController(警报,真实,代表
{
// [2座]
});
// [3座]
返回source.Task;
}
第一次尝试
下面的代码不能正常工作。
-
当我把代码放在
[块1]
或[2座]
- 它不工作
-
当我把代码放在
[3座]
- 只适用于首秀
ActionSheet
。从第二次,它不工作
- 只适用于首秀
UILabel.AppearanceWhenContainedIn(typeof运算(UIActionSheet))字体
= UIFont.FromName(StyleResources.MediumFontName,20)。
第二次尝试
下面的代码也不能正常工作。
-
当我把代码放在
[2座]
- 显示默认的字体很短的时间,显示自定义字体
$之后b $ b -
当我把代码放在
[3座]
- 它的工作只有
取消
按钮
- 它的工作只有
FindDescendantViews<的UILabel>()
是的UIView
A扩展方法,并返回所有子查看相应类型的
VAR标签= alert.View.FindDescendantViews<&的UILabel GT;();
的foreach(在标签VAR标签)
{
label.Font = UIFont.FromName(StyleResources.MediumFontName,20);
}
我这里有解决方案。我创建了一个递归遍历所有子视图运行寻找UILabels,然后设置主题attributedText他们为标题,信息和不同类型的操作的自定义AlertThemeConfigurator。 。随意适当风格的归结串
类AlertThemeConfigurator {
类FUNC configureAlertViewController(alertController: UIAlertController){
AlertLabelConfigurator.adjustLabels(inView:alertController.view,alertController:alertController)
}
类AlertLabelConfigurator {
类FUNC adjustLabels(inView观点:的UIView,alertController:UIAlertController){
为子视图中view.subviews {
如果子视图是的UILabel {
adjustLabel((子视图作为的UILabel!),inAlertViewController:alertController)
}
adjustLabels(inView:子视图,alertController:alertController)
}
}
类FUNC adjustLabel(标签:的UILabel,inAlertViewController alertController:UIAlertController){
如果label.text == {alertController.title
label.attributedText = attributedTitle(label.text)
}否则,如果label.text == {alertController.message
label.attributedText = attributedBody(label.text!)
}
为行动alertController.actions {
如果label.text == action.title {
开关action.style {
情况下.DEFAULT:
label.attributedText = attributedDefaultAction(!action.title)
情况下.Cancel:
label.attributedText = attributedCancelAction(动作.title伪)
情况下.Destructive:!
label.attributedText = attributedDestructiveAction(action.title)
}
}
}
}
类FUNC attributedTitle(标题:字符串) - GT; NSAttributedString {
让属性= [NSFontAttributeName:UIFont(名称:艾文莉介质,尺寸:20)!NSForegroundColorAttributeName:UIColor.greenColor()]
返回NSAttributedString(字符串:标题,属性:属性)
}
类FUNC attributedBody(标题:字符串) - GT; NSAttributedString {
让属性= [NSFontAttributeName:UIFont(名称:艾文莉介质,尺寸:12)!NSForegroundColorAttributeName:UIColor.orangeColor()]
返回NSAttributedString(字符串:标题,属性:属性)
}
类FUNC attributedDefaultAction(标题:字符串) - GT; NSAttributedString {
让属性= [NSFontAttributeName:UIFont(名称:艾文莉介质,尺寸:14)!NSForegroundColorAttributeName:UIColor.yellowColor()]
返回NSAttributedString(字符串:标题,属性:属性)
}
类FUNC attributedCancelAction(标题:字符串) - GT; NSAttributedString {
让属性= [NSFontAttributeName:UIFont(名称:艾文莉介质,尺寸:14)!NSForegroundColorAttributeName:UIColor.purpleColor()]
返回NSAttributedString(字符串:标题,属性:属性)
}
类FUNC attributedDestructiveAction(标题:字符串) - GT; NSAttributedString {
让属性= [NSFontAttributeName:UIFont(名称:艾文莉介质,尺寸:14)!NSForegroundColorAttributeName:UIColor.redColor()]
返回NSAttributedString(字符串:标题,属性:属性)
}
}
}
要展示它叫
让警报= CustomAlertController(标题:标题,留言:信息,preferredStyle:UIAlertControllerStyle.ActionSheet)
alert.addAction(UIAlertAction(标题:关闭,风格:UIAlertActionStyle.Default,处理:无))
presentViewController(警报,动画:真实,完整度:无)
AlertThemeConfigurator.configureAlertViewController(警报)
UIAlertController custom font doesn't work.
The following code is a function ShowActionSheetAsync
, show ActionSheet
.At this point, I want to change the font of ActionSheet
. I have tried several ways, but it didn't work well. Is there good solution?
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;
}
First attemptThe following code does not work properly.
When I put the code on
[Block 1]
or[Block 2]
- It does not work at all
When I put the code on
[Block 3]
- Applies only when the first show
ActionSheet
. From the second times, it does not work
- Applies only when the first show
UILabel.AppearanceWhenContainedIn(typeof(UIActionSheet)).Font = UIFont.FromName(StyleResources.MediumFontName, 20);
The second attemptThe following code also does not work properly.
When I put the code on
[Block 2]
- After showing the default font for a short time, showing the custom font
When I put the code on
[Block 3]
- it work on only
cancel
button
- it work on only
FindDescendantViews<UILabel>()
is a extension method for UIView
and returns all child view of appropriate type.
var labels = alert.View.FindDescendantViews<UILabel>();
foreach (var label in labels)
{
label.Font = UIFont.FromName(StyleResources.MediumFontName, 20);
}
I have the solution here. I created a custom AlertThemeConfigurator that recursively runs through all the subviews looking for UILabels, then sets the themed attributedText on them for the title, message, and different types of actions. Feel free to style the attributed strings appropriately.
class AlertThemeConfigurator {
class func configureAlertViewController(alertController : UIAlertController) {
AlertLabelConfigurator.adjustLabels(inView: alertController.view, alertController: alertController)
}
class AlertLabelConfigurator {
class func adjustLabels(inView view : UIView, alertController : UIAlertController) {
for subview in view.subviews {
if subview is UILabel {
adjustLabel((subview as! UILabel), inAlertViewController : alertController)
}
adjustLabels(inView :subview, alertController : alertController)
}
}
class func adjustLabel(label : UILabel, inAlertViewController alertController : UIAlertController) {
if label.text == alertController.title {
label.attributedText = attributedTitle(label.text!)
} else if label.text == alertController.message {
label.attributedText = attributedBody(label.text!)
}
for action in alertController.actions {
if label.text == action.title {
switch action.style {
case .Default:
label.attributedText = attributedDefaultAction(action.title!)
case .Cancel:
label.attributedText = attributedCancelAction(action.title!)
case .Destructive:
label.attributedText = attributedDestructiveAction(action.title!)
}
}
}
}
class func attributedTitle(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 20)!, NSForegroundColorAttributeName : UIColor.greenColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedBody(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 12)!, NSForegroundColorAttributeName : UIColor.orangeColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedDefaultAction(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.yellowColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedCancelAction(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.purpleColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedDestructiveAction(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.redColor()]
return NSAttributedString(string: title, attributes: attributes)
}
}
}
To present it call
let alert = CustomAlertController(title: "Title", message:"Message" , preferredStyle: UIAlertControllerStyle.ActionSheet)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
AlertThemeConfigurator.configureAlertViewController(alert)
这篇关于UIAlertController自定义字体完全不工作的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!