在我的iOSproject中,我有一个UIAlertView
,上面有3个按钮,但最后一个按钮文本默认为粗体字体,如何...,如何将第三个按钮文本的字体样式设置为正常?
以下是用于创建警报视图的代码
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"test" message:@"testTestTest" delegate:Nil cancelButtonTitle:nil otherButtonTitles:@"first",@"second",@"third" ,nil];
[alert show];
答案 0 :(得分:2)
我花了一些时间在这上面,并在UIAlertView呈现时去挖掘当前视图控制器的子视图结构。我能够为所有按钮文本显示正常字体。
我创建了UIAlertView的子类:
标题文件:
#import <UIKit/UIKit.h>
@interface MLKLoadingAlertView : UIAlertView
- (id)initWithTitle:(NSString *)title;
@end
实施档案:
#import "MLKLoadingAlertView.h"
#define ACTIVITY_INDICATOR_CENTER CGPointMake(130, 90)
@implementation MLKLoadingAlertView
- (id)initWithTitle:(NSString *)title
{
if ( self = [super init] )
{
self.title = title;
self.message = @"\n\n";
[self addButtonWithTitle:@"OK"];
[self addButtonWithTitle:@"Cancel"];
[self addButtonWithTitle:@"Sure"];
[self setDelegate:self];
}
return self;
}
// You can Customise this based on your requirement by adding subviews.
- (void)didPresentAlertView:(UIAlertView *)alertView
{
NSArray *subviews = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController.view.subviews;
if( subviews.count > 1 )
{
// iOS while presenting an alertview uses a presening view controller. That controller's view has several subviews. I have picked one
// subview from it which has frame similar to the alertview frame.
UIView *presentedView = [subviews objectAtIndex:1];
for( UIView *view in presentedView.subviews )
{
for( UIView *subView in view.subviews )
{
if( [subView isKindOfClass:[UITableView class]] )
{
NSInteger numberOfButtons = [(UITableView *)subView numberOfRowsInSection:0];
UITableViewCell *buttonCell = [(UITableView *)subView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:numberOfButtons-1 inSection:0]];
for( UIView *cellSubView in buttonCell.contentView.subviews )
{
if( [cellSubView isKindOfClass:[UILabel class]] )
{
[(UILabel *)cellSubView setFont:[UIFont systemFontOfSize:17]];
}
}
break;
}
}
}
UIActivityIndicatorView *customActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[customActivityIndicator startAnimating];
customActivityIndicator.center = ACTIVITY_INDICATOR_CENTER;
[presentedView addSubview:customActivityIndicator];
}
}
我在我的View Controller中创建了这个MLKLoadingAlertView的实例,如下所示:
MLKLoadingAlertView *loadingAlertView = [[MLKLoadingAlertView alloc] initWithTitle:TITLE];
[loadingAlertView show];
这将显示以下输出:
使用这种方法我们可以实现两件事:
注意:此方法仅适用于iOS 7。
答案 1 :(得分:1)
cancelButtonTitle 取消按钮的标题为空,它将显示如下图像
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
message:@"This is your first UIAlertview message."
delegate:self
cancelButtonTitle:@" "
otherButtonTitles:@"Button 1",@"Button 2", @"Button 3", nil];
[message show];
答案 2 :(得分:0)
标准UI控件的未记录的子视图结构
@interface:UIViewController<UIAlertViewDelegate>
- (void)willPresentAlertView:(UIAlertView *)alertView{
for(UIView *view in alertView.subviews)
if(([view isKindOfClass:[UIButton class]]) && (view.tag ==3)){
UIButton *myButton = (UIButton *)view;
[myButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]];
}
}
或
how-to-change-uialertview-fontcolors
-(void)willPresentAlertView:(UIAlertView *)a;ertView{
UILabel *title = [alertView valurForKey:@"_titleLabel"];
title.font = [UIFont fontWithName:@"Arial" size:18];
[title setTextColor:[UIColor whiteColor];
UILabel *body = [alertView valueForKey:@"_bodyTextLabel"];
body.font = [UIFont fontWithName:@"Arial" size:15];
[body setTextColor:[UIColor whiteColor];
}
答案 3 :(得分:0)
如果您没有获得解决方案或遇到问题,请尝试自定义警报视图。这很容易使用打击我申请链接