Text Alignment issue when I set the custom font - ios

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 !!

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.

Related

How can I trim the height of UILabel to perfectly fit the text without ever changing font size?

Here is my UILabel (defined by what is in the yellow):
Here is after I add [shortDescriptionLabel sizeToFit]; to end of my code:
Here is a second example (to show that the font size is actually getting changed after sizeToFit:
Here is after I add [shortDescriptionLabel sizeToFit]; to end of my code (it is clear that the font size reverted back to size 30.0):
I would like to trim the excess height (above and below carrots). How can I do this? Here is my current code:
shortDescriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(itemImageView.frame.origin.x+itemImageView.frame.size.width+10, 20, self.tableView.frame.size.width-itemImageView.frame.origin.x- itemImageView.frame.size.width-20-20, tableViewCellHeight/2)];
shortDescriptionLabel.text = itemObject.shortDescription;
shortDescriptionLabel.font = [UIFont boldSystemFontOfSize:30];
shortDescriptionLabel.textAlignment = NSTextAlignmentCenter;
shortDescriptionLabel.adjustsFontSizeToFitWidth = YES;
shortDescriptionLabel.numberOfLines = 0;
shortDescriptionLabel.minimumScaleFactor = 0;
[shortDescriptionLabel setBackgroundColor:[UIColor yellowColor]];
CGFloat fontSize = shortDescriptionLabel.font.pointSize;
NSLog(#"fontSize = %f", fontSize);
Keep in mind that the last NSLog will always display 30.0 (the font size set earlier) even if the displayed font size is obviously smaller than 30.0.
Please help. There seems to be a lot about this online but I can't seem to get anything to work.
Yoh can use sizeToFit but if you want to trim the height and keep the width you can add a height constraint equal to the used UIFont's lineHeight.
AutoLayout
1-Properly Add its Leading,Trailing,Top and bottom Constraints and Write this line of code will do
[label setPreferredMaxLayoutWidth:300.0];
Non AutoLayout
-(CGSize)m_GetHeight:(NSString*)text
{
CGSize constraintSize=[text sizeWithFont:[UIFont fontWithName:#"Arial" size:15.] constrainedToSize:CGSizeMake(182, 2000) lineBreakMode:NSLineBreakByWordWrapping];
return constraintSize;
}

how to programmatically align a label to an x, y coordinate

below are some other properties of the label. obviously nstextalignmentleft is not what i'm going for. having trouble understanding where to enter coordinates.
self.lblTimer = [[UILabel alloc] init];
self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
self.lblTimer.textAlignment = NSTextAlignmentLeft;
self.lblTimer.font = [UIFont systemFontOfSize:10];
self.lblTimer.textColor = [UIColor redColor];`self.lblTimer = [[UILabel alloc] init];
I think you're misunderstanding what textAlignment does. This simply controls the alignment within the label, not on the screen. If you want to position the label on screen you must change its frame property:
self.lblTimer.frame = CGRectMake(x,y,width,height);
As you've set self.lblTimer.translatesAutoresizingMaskIntoConstraints to NO I am guessing you want to use Auto Layout and if so, I highly recommend taking a look at Masonry.
An alternative is to set the frame property on the label with requires a CGRect where you give it the x and y coordinates as well as the width and height.

How can I make a UITextView layout text the same as a UILabel?

I have a UILabel that I need to convert to a UITextView because reasons. When I do this, the text is not positioned the same, despite using the same (custom) font.
I found that if I set:
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
This gets the text very close, but if I superimpose the UITextView over top of the UILabel, I see the text positioning get farther apart with each new line.
The UILabel is green, the UITextView is black. This is using NSParagraphStyle to set min and max line height to 15.
I've played with setting the paragraph style and min/max line height, but I haven't been able to match it exactly. I'm not a printer, so I don't necessarily understand all of the font related terms in the documentation for NSLayoutManager and NSTextContainer and all that.
I only need to support iOS 7 and up.
I'm not going to switch to some crazy CoreText-based custom widget or use some random third party library. I'm okay with close enough if I have to. But it seems like there should be some combination of random properties to make them layout the same.
I took the solution for line spacing found at this link and applied it to your issue. I managed to get it incredibly close by adjusting the lineSpacing property. I tested with HelveticaNeue size 13 and managed to get it to line up as shown in the screen shot below.
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -0.38;
NSDictionary *attrsDictionary =
#{ NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:13.0f],
NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attrsDictionary];
I've been able to successfully 'impersonate' a non-editable multiline UILabel (as it happens, in a UITableViewCell subclass) with an equivalent editable multiline UITextView using the following :
_textView = UITextView.new;
_textView.font = _label.font;
_textView.textColor = _label.textColor;
_textView.textAlignment = _label.textAlignment;
_textView.backgroundColor = UIColor.clearColor;
_textView.textContainer.lineFragmentPadding = 0;
_textView.textContainerInset = UIEdgeInsetsZero;
and to make it behave well when doing actual edits, add the following to your UITextViewDelegate:
- (void)textViewDidChange:(UITextView *)textView
{
...
[textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
[textView scrollRectToVisible:[textView caretRectForPosition:textView.endOfDocument] animated:NO];
}

How to use NSLineBreakByCharWrapping with setNumberOfLines:1 on iOS?

For example, if the text for a UILabel is: "Questions that may already have your answer", I want it to print
"Questions that may alr". I don't want to clip the last character but cut the text at that point.
I cannot use a character limit as most fonts are not monospaced. I cannot use clipping as It may cut the text at another point or it may cut that last letter "r" from any where of it.
The behaviour I want is similar to NSLineBreakByCharWrapping while numberOfLines = 0.
So, I want it to drop(wrap) that non-fitting last character but I want it to drop/wrap to a hidden space = I don't want that second line.
How can this be possible?
To get required output you need to know width of the label, which can be known by following method:
CGSize size = [string sizeWithAttributes:
#{NSFontAttributeName:
[UIFont systemFontOfSize:17.0f]}]; // iOS 7 and later
CGSize size = [string sizeWithFont: [UIFont systemFontOfSize:17.0f]]; // prior to iOS 7
set the size of your label with the above calculated size.
set the numberOfLines property to 1;
set the lineBreakMode property to NSLineBreakByCharWrapping;
UILabel *yourLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, size.width, 50)];
placeLbl.numberOfLines = 1;
placeLbl.lineBreakMode = NSLineBreakByCharWrapping;
In swift 3.0
labelObject.numberOfLines = 0;
labelObject.lineBreakMode = NSLineBreakMode.byCharWrapping

iOS: sizeWithFont: for custom font

I've been plugging away and I need to use the sizeWithFont: method to properly line up my layout, but it doesn't seem to work with custom fonts. Is there a way to get it to work, or maybe another method I can use?
I'm pretty stumped on this. Any help is appreciated.
sizeWithFont should definitely work, but if it's giving you a problem then there is a workaround. You can put the text in a UITextView, add it to your layout, and then retrieve the actual text size and adjust accordingly. Here's some sample code from one of my projects:
// Put in the title in
self.titleView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 195, 195)];
self.titleView.font = [UIFont fontWithName:#"Bliss Pro" size:20.0];
[self addSubview:self.titleView];
self.titleView.text = #"Add your text here";
// Now get the actual size of the text and resize the title view's frame
CGRect frame = self.titleView.frame;
frame.size.height = self.titleView.contentSize.height;
self.titleView.frame = frame;
It's a bit hackish, but it certainly works.
#define kFontSize 14.0 //modify your font size
CGSize size = [yourView sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];

Resources