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

9

我有一个标题为“DO: These tasks”的UIActionSheet。在标题字符串中,“DO:”子字符串应该是粗体(具有特定的字体大小),而“这些任务”子字符串应该是常规字体。这是否可能?我该如何做到这一点?

6个回答

30

我假设你有一个实现了 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属性中的一部分。


现在,您可以添加一个新的UILabel,并使用不同的字体为您的actionsheet,如果您想要看到粗体的DO:字符串...但从这里开始,只有您的想象力是极限了,在此方法或-didPresentActionSheet:方法中,您可以格式化UIActionSheet的元素。

所以这就是这个问题的解决方案

更新于2013年10月7日

在iOS7中,我们实际上无法直接访问UIAlertViewUIActionSheet对象的子视图,因此此解决方案在iOS7环境下可能无法正常工作。

如果您在iOS7上有任何问题,请给我留言!谢谢。

更新于2014年6月22日

我没有找到任何直接使用新UIAlertController在iOS8中进行此类操作的解决方案,标题标签似乎在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)
    })

注意:将自定义内容添加到提示框/操作表可能很有用,但如果将来不起作用,我将更新我的答案。


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

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

由于某种原因,这不会更改取消按钮的字体 :( - samvermette
嗨@samvermette,上面的内容已经过测试,并在我的一个应用程序中使用:)一切正常。如果您找到任何其他解决方案,请务必更新。 - icodebuster
这似乎只调整了取消按钮。我不知道为什么其他按钮没有改变... - Colin Tremblay
@icodebuster iOS 7。在iOS 8上,字体大小会自动调整以适应屏幕,所以我不需要担心它。 - Colin Tremblay
@VladimírSlavík 如果您发现答案有用,请点赞:) - icodebuster
显示剩余5条评论

6

基于Holex的回答:

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

以上内容似乎适用于iOS6和iOS7模拟器。但我不确定在未来的其他iOS版本中是否有效。


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

}

0

UIActionSheet有一个叫做_titleLabel的内部属性,因此可以获取其引用并直接更改标签的属性字符串。

请注意,这是一个私有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];

    }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接