Text Alignment issue when I set the custom font

When I set the custom font for the segmented control then it changes the vertical text alignment. I am using below code to set the font .

    // I dont think these lines are creating any issue but just wanted to paste all the code
   self.segmentType.layer.borderColor = navigationTintColor.CGColor;
   self.segmentType.layer.cornerRadius = 0.0;
   self.segmentType.layer.borderWidth = 1.5;

   // These are the lines that are changing the text alignment
    UIFont *font = [UIFont fontWithName:ftHelveticaNeueLTPro_Th size:13.5];        
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                           forKey:UITextAttributeFont];
    [self.segmentType setTitleTextAttributes:attributes
                                     forState:UIControlStateNormal];

Here is the screenshot of whats is happening . If you observer, the text is not vertically centre aligned .

  • UISegmentedControl only changes text color when revisiting ViewController
  • Change border color of UISegmentedControl
  • UINavigationBar with UISegmentedControl partially covers childViews
  • Remove UISegmentedControl separators completely. (iphone)
  • iphone ios7 segmented UISegmentedControl change only border color
  • Customize UISegmentedControl, add background image and selected segment tint color
  • enter image description here

    Please help me . Thank you in advance !!

    3 Solutions Collect From Internet About “Text Alignment issue when I set the custom font”

    The below code suggested by @Emmanuel works perfectly fine. You can change the vertical offset to align the text vertically at the center .

     [self.segmentType setContentPositionAdjustment:UIOffsetMake(0, 2) forSegmentType:UISegmentedControlSegmentAny barMetrics:UIBarMetricsDefault];
    

    Can you please try it using custom UILabel on custom view on it. Change & modify frame value of either titleLabel or customSegmentView as per convenience on actual view. And add this whole view as subview on your segmented control.

    UIView *customSegmentView = [[UIView alloc] init];
        UILabel *segmentTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 7.0f,180.0f,22.6f)];
        segmentTitleLabel.text = @"your-text";       
        segmentTitleLabel.backgroundColor = [UIColor clearColor];
        segmentTitleLabel.textAlignment = UITextAlignmentCenter;
        segmentTitleLabel.font = [UIFont fontWithName:@"ftHelveticaNeueLTPro_Th" size:13.5f];
        customSegmentView.frame = CGRectMake(60, 20, 180, 35);
        [customSegmentView addSubview:segmentTitleLabel];
        [self.segmentType setTitleView:customSegmentView];
    

    Hope that will work for your issue. Please check and let me know if we have to go with another solution.

    In InterfaceBuilder on XCode 6 there is a Content Offset control for the segments, which affects the baseline of the text. I had this problem because my Content Offset was 2 in the Y dimension instead of 0.