更改UIActionSheet标题string的字体types和大小

我有一个标题string“DO:这些任务”的UIActionSheet。 在标题string中,子string“DO:”应该是粗体(具有特定的字体大小),子string“这些任务”应该是正则的。 可能吗? 我怎样才能做到这一点?

我假设你有一个实现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]]; } } } 

但只要你看到你没有机会格式的UILabel对象的text属性的一部分。


你现在可以用不同的字体为你的actionsheet添加一个新的UILabel ,如果你想看到一个带有粗体的DO:string的文本…但是从这里开始,极限只是你的想象,你可以把UIActionSheet的元素此方法或在-didPresentActionSheet:方法中。

所以这将是这个想法

2013年10月7日更新

在iOS7中,我们并没有直接访问 UIAlertView UIActionSheet 对象 的子视图 ,所以此解决scheme可能无法在iOS7环境中正常工作。

如果你在iOS7上有任何问题,请给我评论! 谢谢。

2014年6月22日更新

我还没有find任何解决scheme来直接在UIAlertController中使用新的UIAlertController来做这样的事情,标题标签看起来在UIAlertController的整个视图层次结构中是不可见的,在它出现之前甚至在之后UIAlertController不可见的。

注意:我也发现在屏幕上出现之后不禁止重叠/覆盖它的内容。 目前无法预测它可以在Beta版本上运行,或者它是否是AppStore安全的。

注意:请牢记视图生命周期的时间表,并且不要试图在视图或视图控制器中呈现任何内容(如果视图不在导航堆栈中)。

无论如何,这是一个Swift解决scheme,我可以如何到达图层,我可以添加我的自定义视图。

 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) }) 

注意:将自定义内容添加到警报/操作单可能会很有用,但是如果将来无法使用,我会更新我的答案。

对于iOS 7 ,以下代码将起作用。

实现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]]; } }]; } 

对于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]]; } } } 

基于Holex的回答:

对于iOS7和iOS6,以下内容对我来说是改变了UIActionSheet属性的标题。 请注意,我实际上是添加一个新的子视图,因为对于未知的? 更新当前UILabel的原因只给出垂直文本的一半? 另外“标题”显然是第一个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; } } } 

以上似乎在iOS6和iOS7模拟器上工作。 我不知道这是否会与其他未来版本的iOS工作。

以下可能有用。

 - (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]; } }]; } 

看到这些链接,并使用它。 ohattributedlabel

http://www.cocoacontrols.com/platforms/ios/controls/ohattributedlabel

UIActionSheet的内部属性名为_titleLabel因此可以直接获取引用并更改此标签的属性string属性。

请注意,这是一个私有API,可能会在App Store中被拒绝。

下面是它的工作原理:

 - (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]; } }