2012-07-24 166 views

回答

28

我假设你有一个实现了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:字符串文本......但是从这里的限制只是你的想象力,你可以格式化该方法中的UIActionSheet的元素或-didPresentActionSheet:方法也是如此。

因此这将是这个想法

UPDATE于2013年10月7日

在iOS7 我们真的没有一个UIAlertViewUIActionSheet对象的子视图直接访问,因此这种解决方案可能不会对iOS7环境正常工作。

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

UPDATE在2014年6月22日

我还没有找到任何解决办法直接与iOS8上新UIAlertController做这种事,标题标签看起来是在UIAlertController的整个视图层次结构不可见,无论是可见之前,甚至之后。

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

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

反正这里是一个斯威夫特的解决方案,我怎么能到达层,我可以我自定义视图添加到它。

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

注:这可能是自定义内容添加到一个警报/ actionsheet有用的,但是如果它不能在今后的工作中,我会更新我的答案。

+1

它工作在iOS7以及对我来说没有任何问题 – 2014-04-11 14:24:39

6

基于Holex的回答是:

两个iOS7及iOS6的下面很适合我改变UIActionSheet属性的称号。请注意,我实际上添加了一个新的子视图,因为对于未知的?更新当前UILabel的原因只给出垂直文本的一半? 另外“标题”显然是第一个UILabel,所以我们在一次更改后打破循环。

​​

以上似乎适用于iOS6和iOS7模拟器。我不确定这是否可以与其他未来版本的iOS一起使用。

+0

这不适用于iOS 8.3。 :) – 2015-04-09 14:45:30

4

下面的内容可能会有帮助。

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

} 
9

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

由于某种原因,这将不会改变取消按钮的字体:( – samvermette 2014-03-08 03:20:48

+0

嘿@samvermette上面已经过测试,并在我的应用程序中使用:)工作正常。如果您发现任何其他解决方案,请更新。 – icodebuster 2014-03-08 03:37:59

+0

@icodebuster您的解决方案适用于iOS 7,谢谢。 – 2014-05-01 05:24:19

0

有一个叫_titleLabelUIActionSheet内部属性,因此它有可能获得一个参考值和更改属性串属性这个标签直接。

请注意,这是一个私有API,可能会在应用商店被拒绝。

这里是它如何工作的:

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

    } 
} 
相关问题