更改UIActionSheet标题字符串的字体类型和大小

Xav*_*ero 8 iphone font-size uiactionsheet ios

我有一个标题字符串为"DO:这些任务"的UIActionSheet.在标题字符串中,子字符串"DO:"应该是粗体(具有特定的字体大小),子字符串"这些任务"应该是常规的.可能吗?我怎样才能做到这一点?

hol*_*lex 29

我假设你有一个实现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]];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但据你所知,你没有机会格式化UILabel对象text属性的一部分.


你可以用不同的字体UILabel为你actionsheet现在添加一个新的,如果你想看到一个带有粗体DO:字符串的文本......但是从这里开始限制只是你想象的,你可以格式化UIActionSheet这个方法或里面的元素-didPresentActionSheet:方法也是如此.

所以这就是这个想法.

2013年10月7日更新

在iOS7中,我们实际上无法直接访问某个 UIAlertView 或某个 对象的子视图UIActionSheet ,因此该解决方案可能无法在iOS7环境中正常运行.

如果您在iOS7上遇到任何问题,请向我发表评论!谢谢.

2014年6月22日更新

我没有找到任何解决方案直接用UIAlertControlleriOS8中的new做这样的事情,标题标签在整个视图层次结构中看起来不可见,UIAlertController在它出现之前或之后都不可见.

注意:我还发现在屏幕上显示内容,禁止重叠/覆盖其内容.目前无法预测它是否可以在Beta上运行,或者它是否是AppStore安全的.

注意:请记住视图生命周期的日程安排,如果不在导航堆栈中,请不要尝试在视图中显示任何内容或查看控制器.

无论如何,这是一个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)
    })
Run Code Online (Sandbox Code Playgroud)

注意:将自定义内容添加到警报/操作表可能很有用,但如果将来无效,我将更新我的答案.


ico*_*ter 10

对于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]];
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

适用于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]];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Sas*_*sho 6

根据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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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