如何在中间(iOS)对齐自定义UINavigationBar标题(UILabel)

时间:2015-05-06 04:45:02

标签: ios objective-c uiview uilabel uinavigationbar

我正在使用以下代码在UINavigationBar中心对齐2个字符串。

代表:

         <Back     John Kelly Maria
                       23/F

我需要在UINavigationbar中使用这些中间对齐的文本,但现在获得的是所有文本在UINavigationBar的右侧对齐。  像这样的东西

      <Back                  John Kelly Maria   
                                  23/F

请帮帮我。 这是我的代码

 UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)];

    UIFont * customFont = [UIFont fontWithName:@"Helvetica Neue" size:19]; //custom font
    NSString * text =title;
    UILabel *patientNameLabel;
    if([title length]>15){
        patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
    }else{
        patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];

    }
    patientNameLabel.text = @"John Kelly Maria";
    patientNameLabel.font = customFont;
    patientNameLabel.numberOfLines = 0;
    patientNameLabel.textAlignment=NSTextAlignmentCenter;
    patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
    patientNameLabel.clipsToBounds = YES;
    patientNameLabel.backgroundColor = [UIColor clearColor];
    patientNameLabel.textColor = [UIColor blackColor];
    patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    NSString *subTitle =subTitleText;
    UIFont * customFont1 = [UIFont fontWithName:@"Helvetica Neue" size:14]; //custom font

    UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
    subTitleLabel.textAlignment = NSTextAlignmentCenter;

    subTitleLabel.text = @"23/F";
    subTitleLabel.font = customFont1;
     subTitleLabel.textAlignment=NSTextAlignmentCenter;
    subTitleLabel.numberOfLines = 1;

    subTitleLabel.adjustsFontSizeToFitWidth = YES;
    subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
    subTitleLabel.clipsToBounds = YES;
    subTitleLabel.backgroundColor = [UIColor clearColor];
    subTitleLabel.textColor = [UIColor blackColor];
    [titleView addSubview:patientNameLabel];
    [titleView addSubview:subTitleLabel];

2 个答案:

答案 0 :(得分:0)

在addSubview行之前添加以下两行..

patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 0);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 25);

添加以下代码&amp;见

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];

UIFont * customFont = [UIFont fontWithName:@"Helvetica Neue" size:19]; //custom font
NSString * text =@"Nilesh Patel";
UILabel *patientNameLabel;
if([text length]>15){
    patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
    patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];

}
patientNameLabel.text = @"Nilesh Patel";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =@"iOS Developer";
UIFont * customFont1 = [UIFont fontWithName:@"Helvetica Neue" size:14]; //custom font

UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;

subTitleLabel.text = @"28/M";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;

subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];

patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 10);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 30);


[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
[self.navigationController.navigationBar addSubview:titleView];

注意:您已将静态帧设置为titleView,因此如果您在不同的设备上看到您的应用,则无法在正确的中心看到该文本。为避免这种情况,请根据设备/视图宽度动态设置titleView框架。

答案 1 :(得分:0)

尝试这样的帧设置

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,20,screenWidth,50)];
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,screenWidth,20)];
UILabel *patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,screenWidth,30)];