タイトル文字列「DO:これらのタスク」のUIActionSheetがあります。タイトル文字列では、サブ文字列「DO:」は太字(特定のフォントサイズ)で、サブ文字列「これらのタスク」は通常である必要があります。出来ますか?これどうやってするの?
6 に答える
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:
だからこれはこのためのアイデアになります。
2013年10月7日の更新
UIAlertView
iOS7では、またはオブジェクト のサブビューに直接アクセスできUIActionSheet
ないため、このソリューションはiOS7環境では正しく機能しない可能性があります。
iOS7で問題が発生した場合は、コメントしてください。ありがとうございました。
2014年6月22日の更新
私はiOS8の新しいものでそのようなことを直接行うための解決策を見つけていませんUIAlertController
。タイトルラベルはビュー全体に表示されていないように見えます-の階層、表示さUIAlertController
れる前でも後でも表示されません。
注:画面に表示された後、コンテンツをオーバーラップ/カバーすることは禁止されていないこともわかりました。現在、ベータ版で動作するかどうか、または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)
})
注:アラート/アクションシートにカスタムコンテンツを追加すると便利な場合がありますが、今後機能しない場合は、回答を更新します。
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]];
}
}];
}
iOS6の場合
- (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]];
}
}
}
ホレックスの答えに基づく:
iOS7とiOS6の両方で、UIActionSheetプロパティのタイトルを変更するために次のことが機能しました。不明なため、実際に新しいサブビューを追加していることに注意してください。現在のUILabelを更新すると、テキストの垂直方向の半分しか表示されないのはなぜですか?また、「タイトル」は明らかに最初のUILabelであるため、1回の変更でループが中断されます。
- (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
ため、参照を取得して、このラベルの属性付き文字列プロパティを直接変更できます。
これはプライベートAPIであり、Appストアで拒否される可能性があることに注意してください。
仕組みは次のとおりです。
- (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];
}
}