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 .
Please help me . Thank you in advance !!
- UISegment value changing when tableview get scrolled
- Define click event for UISegmentedControl
- How do I segue to 2 views based on a Segmented Control and an Add button?
- Customizing UISegmentedControl in iOS 5
- Segmented control, change tint opacity, but not border
- How to add the UISegmentedControl in UINavigationBar?
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.
- Getting list of files in documents folder
- NSString is integer?
- Blur UIImage outside of bounds (Photoshop-style)
- Whats is methodLists attribute of the structure objc_class for?
- Always getting the same height using sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
- HTML5 audio playback over HTTPS on safari IPhone 4.3
- OCMock and block testing, executing
- How to make an IPA on XCode 4.3?
- creating JSON format in Objective C
- How to make a designated initializer for NSManagedObject subclass in Swift?
- Hide UISearchBar clear text button
- IPV6 Swift Reachability
- How to make cocos2d sprite scale up and down (for a pulsating effect) every second?
- Variable of AppDelegate used as global variable doesn't work