我有一个标题字符串为“DO:这些任务”的UIActionSheet。在标题字符串中,子字符串“DO:”应该是粗体(具有特定的字体大小),子字符串“这些任务”应该是常规的。可能吗?我怎么能这样做?
答案 0 :(得分: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]];
}
}
}
但据您所见,您无法格式化UILabel
对象text
属性的部分。
如果您希望查看带有粗体UILabel
字符串的文字,则可以使用其他字体为actionsheet
添加新的DO:
...但是从这里开始限制只是你想象的,你可以在这个方法或UIActionSheet
方法中格式化-didPresentActionSheet:
的元素。
所以这就是这个想法。
我们实际上无法直接访问 UIAlertView
或 UIActionSheet
对象的子视图,因此此解决方案可能无法在iOS7环境中正常运行。
如果您在iOS7上遇到任何问题,请向我发表评论!谢谢。
我还没有找到任何解决方案直接使用iOS8中的新UIAlertController
进行此类操作,标题标签看起来在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)
})
注意:将自定义内容添加到警报/操作表可能很有用,但如果以后它不起作用,我会更新我的答案。
答案 1 :(得分: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]];
}
}
}
答案 2 :(得分: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;
}
}
}
以上似乎适用于iOS6和iOS7模拟器。我不确定这是否适用于iOS的其他未来版本。
答案 3 :(得分: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];
}
}];
}
答案 4 :(得分:0)
答案 5 :(得分: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];
}
}