ios 更改 UIActionSheet 标题字符串的字体类型和大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11629113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change font type and size of UIActionSheet title string
提问by Xavi Valero
I have a UIActionSheet with title string "DO: These tasks". In the title string, the substring "DO:" should be Bold(with a particular font size) and the substring "These tasks" should be Regular. Is it possible? How can I do this?
我有一个标题字符串“做:这些任务”的 UIActionSheet。在标题字符串中,子字符串“DO:”应该是粗体(具有特定的字体大小),子字符串“这些任务”应该是常规的。是否可以?我怎样才能做到这一点?
回答by holex
I assume you have a class which implements the UIActionSheetDelegate
protocol and your class is a delegate class of the current UIActionSheet
, so in that class you canchange the whole title of the UIActionSheet
like
我假设你有它实现了一类UIActionSheetDelegate
协议和你的类是委托类的电流UIActionSheet
,所以在这个类,你可以改变的整个标题UIActionSheet
一样
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UILabel class]]) {
[((UILabel *)_currentView) setFont:[UIFont boldSystemFontOfSize:15.f]];
}
}
}
but as far as you see you have no chance to format the part of the UILabel
object's text
property.
但据您所知,您没有机会格式化UILabel
对象text
属性的一部分。
you could add a new UILabel
for your actionsheet
now with a different font, if you want to see a text with the bolded DO:
string... but from here the limit is you imagination only, you can format the UIActionSheet
's elements in this method or inside the -didPresentActionSheet:
method as well.
您可以添加一个新的UILabel
你actionsheet
现在使用不同的字体,如果你想看到粗体文字DO:
串......但是从这里的限制是你的想象力而已,你可以格式化UIActionSheet
这个方法或内部的元素-didPresentActionSheet:
方法也一样。
so this would be the ideafor this.
所以这就是这个想法。
UPDATE on 7 Oct 2013
2013 年 10 月 7 日更新
in iOS7 we don't really have direct access to the subviews of anUIAlertView
or anUIActionSheet
object, so this solution might not be working properly on iOS7 environment.
在 iOS7 中,我们实际上无法直接访问 anUIAlertView
或object的子视图UIActionSheet
,因此此解决方案可能无法在 iOS7 环境中正常工作。
if you have any issue with it on iOS7, please comment it to me! thank you.
如果你在 iOS7 上有任何问题,请给我评论!谢谢你。
UPDATE on 22 Jun 2014
2014 年 6 月 22 日更新
I have not found any solution to do such thing directly with the new UIAlertController
in iOS8, the title label looks to be not visible in the entire view-hierarchy of the UIAlertController
, neither visible before it appears or even after.
我还没有找到任何解决方案来直接使用UIAlertController
iOS8 中的 new 来做这样的事情,标题标签在 的整个视图层次结构中看起来不可见,UIAlertController
在它出现之前甚至之后都不可见。
NOTE: I've also figured out that it is not forbidden to overlap/cover its content, afterit appeared on the screen. currently it is impossible to predict that it will work over Beta or whether or not it is AppStore-safe.
注意:我还发现,在它出现在屏幕上之后,不禁止重叠/覆盖它的内容。目前无法预测它是否会在 Beta 上运行,或者它是否是 AppStore 安全的。
NOTE: please bear in your mind the view-lifecycle's schedule, and do not try to present any content in a view or view controler if that is not in the navigation stack already.
注意:请记住视图生命周期的时间表,并且不要尝试在视图或视图控制器中呈现任何内容,如果这些内容已经不在导航堆栈中。
anyway, here is a Swiftsolution how I could reach the layers and I could add my custom view to it.
无论如何,这是一个Swift解决方案,我可以如何到达图层,并且可以向其中添加自定义视图。
let alertController: UIAlertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alertController, animated: true, completion: {
let aboveBlurLayer: UIView = alertController.view.subviews[0] as UIView
let belowBlurLayer: UIView = (aboveBlurLayer.subviews[0].subviews as Array<AnyObject>)[0] as UIView
let customView: UIView = UIView(frame: aboveBlurLayer.bounds)
customView.backgroundColor = UIColor.redColor()
aboveBlurLayer.addSubview(customView)
})
NOTE: it might be useful to add custom content to an alert/actionsheet, but if it does not work in the future, I will update my answer.
注意:将自定义内容添加到警报/操作表可能很有用,但如果将来不起作用,我将更新我的答案。
回答by icodebuster
For iOS 7the following code will work.
对于 iOS 7,以下代码将起作用。
Implement UIActionSheetDelegate
as follows
实现UIActionSheetDelegate
如下
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
[actionSheet.subviews enumerateObjectsUsingBlock:^(id _currentView, NSUInteger idx, BOOL *stop) {
if ([_currentView isKindOfClass:[UIButton class]]) {
[((UIButton *)_currentView).titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
// OR
//[((UIButton *)_currentView).titleLabel setFont:[UIFont fontWithName:@"Exo2-SemiBold" size:17]];
}
}];
}
For iOS 6
对于 iOS 6
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UILabel class]]) {
[(UILabel *)_currentView setFont:[UIFont boldSystemFontOfSize:15.f]];
// OR
//[(UILabel *)_currentView setFont:[UIFont fontWithName:@"Exo2-SemiBold" size:17]];
}
}
}
回答by Sasho
Based on Holex's answer:
基于 Holex 的回答:
for both iOS7 and iOS6 the following has worked for me to change the title of UIActionSheet properties. Please note that I am actually adding a new subview, since for unknown? reason updating the current UILabel gives only vertically half of the text? Also "title" is apparantly the first UILabel, so we break the loop after one change.
对于 iOS7 和 iOS6,以下对我来说改变了 UIActionSheet 属性的标题。请注意,我实际上是在添加一个新的子视图,因为未知?更新当前 UILabel 的原因仅提供垂直一半的文本?此外,“title”显然是第一个 UILabel,因此我们在一次更改后中断循环。
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UILabel class]]) {
UILabel *l = [[UILabel alloc] initWithFrame:_currentView.frame];
l.text = [(UILabel *)_currentView text];
[l setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
l.textColor = [UIColor darkGrayColor];
l.backgroundColor = [UIColor clearColor];
[l sizeToFit];
[l setCenter:CGPointMake(actionSheet.center.x, 25)];
[l setFrame:CGRectIntegral(l.frame)];
[actionSheet addSubview:l];
_currentView.hidden = YES;
break;
}
}
}
The above seems to work on iOS6 and iOS7 simulators. I am not sure if this would work with other future versions of iOS though.
以上似乎适用于 iOS6 和 iOS7 模拟器。不过,我不确定这是否适用于其他未来版本的 iOS。
回答by Ramesh
Following may be useful.
以下可能有用。
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
[actionSheet.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)obj;
button.titleLabel.font = [UIFont systemFontOfSize:15];
}
}];
}
回答by M.B
see these link .and use it. ohattributedlabel
看到这些链接。并使用它。 属性标签
http://www.cocoacontrols.com/platforms/ios/controls/ohattributedlabel
http://www.cocoacontrols.com/platforms/ios/controls/ohattributedlabel
回答by Alexander
There is an internal property of UIActionSheet
called _titleLabel
so it is possible to get a reference and change the attributed string property of this label directly.
有一个UIActionSheet
调用的内部属性,_titleLabel
因此可以获取引用并直接更改此标签的属性字符串属性。
Please be aware, that this is a private API in might be rejected in the App store.
请注意,这是一个私有 API,可能会在 App Store 中被拒绝。
Here is how it works:
下面是它的工作原理:
- (NSAttributedString *) actionSheetAttributedTitle;
{
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@"Test"];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:13.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attString.length)];
[attString addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, attString.length)];
return [attString copy];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
{
UILabel *sheetTitleLabel;
if([actionSheet respondsToSelector:@selector(_titleLabel)]) {
sheetTitleLabel = objc_msgSend(actionSheet, @selector(_titleLabel));
sheetTitleLabel.attributedText = [self actionSheetAttributedTitle];
}
}