Localized UILabel with background - ios

I have an app in two languages (English and German) and app contains labels with background. In English everything is ok but in German UILabels are to short and text which is in it is shorter with 3 dots. Like:
Can I add some auto-resize property which resize label to correct size? Last one thing is that I have a lot of labels interface-only. I want to save some time and figure it out in interface. Is it possible or the only way is set sizeToFit to labels? Thank you for advices.

So what you want to do is, set the sizeToFit in the interface.
It is quite easily possible.
Set the AutoShrink Property in Attributes inspector, and it will be equivalent to size to fit.
Further you can even Specify the minimum font size and font scale.

Kakshil's answer will work for the case that you're not wanting to resize the label to actually fit the contained text. It's going to reduce the point size of the text in an attempt to fit the available space. For truly dynamic text that may be the best option.
On the other hand, if you're wanting to actually resize the label to fit the text then you can call [label sizeToFit] after you change the label text. Alternatively, you can use sizeThatFits: to use a modified bounds, where you don't want the label to grow too big or too small.

I customised my label, add localization and set frame with inset. How's simple. But one disadvantage is that I have to set tag as key. My solution is:
- (void)layoutSubviews {
NSString *key = [NSString stringWithFormat:#"%d", self.tag];
self.text = NSLocalizedString(key, nil);
CGSize textSize = [self.text sizeWithAttributes:#{NSFontAttributeName:self.font}];
CGFloat strikeWidth = textSize.width;
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, strikeWidth + 20, self.frame.size.height)];
[super layoutSubviews];
}
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

Related

How to adjust UILabel font size to fit the fixed Rectangle (for iOS7 +)?

I know this question has been asked so many times and I have seen most of the solutions like
How to adjust font size of label to fit the rectangle?
How to calculate actual font point size in iOS 7 (not the bounding rectangle)?
NSString sizeWithFont: alternative in iOS7
What I want is that I want to adjust the font size in a given rect with WORD WRAPPING technique. The posts I mentioned above somehow adjust the font size but none of them is using word wrapping technique. Let say, I have a label
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 25)]; \\60px width
tempLabel.text = #"Internationalisation";
tempLabel.numberOfLines = 2;
Now it should wrap the text in one line (within 60 px) with minimum possible font. But it keeps the text in 2 lines with a bit bigger font and the text breaks up like "Internationali" in first line and "sation" in second. How can we apply word wrapping in it.
tempLabel.adjustsFontSizeToFitWidth = YES;
does not seem to work for two or more lines and
CGSize size = [self sizeWithFont:font
minFontSize:minFontSize
actualFontSize:&actualFontSize
forWidth:maxWidth
lineBreakMode:self.lineBreakMode];
is deprecated in iOS7+
So how can I resolve my Issue. Please do help. This is so important for me.

Set UILabel Align top is not working in the cell

I have a problem to implement the text vertical alignment inside a table cell.
What I want to do is based on the length of the text I want to display a message top aligned in side one UILabel inside a cell.
For example if the message is only one line
The text should align top:
And if there are two rows then it should look like this:
At the beginning what I can see is like this
So I have searched the web and what I found is to
use the
[label1 sizeToFit];
But the problem with that is within the table view cell it is not always necessarily called especially when I switched to and from another tab view.
Then I tried to generate the label on the fly by code, but the problem is that let alone the complicated process of setting up the font format I want. I have to manage whether the label has been inserted or not then reuse it every time cellForRowAtIndexpath is called.
And more weirdly, once I select the row. The alignment is switched from the one you see in the first picture to the third one. It also happens when I switched to a different tab view and switch back to the view.
I was wondering if anybody has encountered such issue and have a solution to the problem.
Thank you for your reply in advance.
Edit:
#βḧäṙℊặṿῗ, what you said I have tried. It successfully align the label text if there is only one line. My situation is that, since I have multiple tab views. Once I switch back and forth between tabs view. The alignment just restored to centre-vertical alignment again. It also happens when I selected the row. Any idea?
Try this
// label will use the number of lines as per content
[myLabel setNumberOfLines:0]; // VERY IMP
[myLabel sizeToFit];
EDIT:
As you have one extra condition that maximumly display two lines then you need to set setNumberOfLines: to 2
[myLabel setNumberOfLines:2];
Create UILabel+Extras and add following methods to this class.
- (void)alignTop{
CGSize fontSize = [self.text sizeWithAttributes:#{NSFontAttributeName:self.font}];
double finalHeight = fontSize.height * self.numberOfLines;
double finalWidth = self.frame.size.width; //expected width of label
CGRect rect = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options:NSStringDrawingTruncatesLastVisibleLine attributes:#{NSFontAttributeName:self.font} context:nil];
CGSize theStringSize = rect.size;
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=0; i< newLinesToPad; i++)
self.text = [self.text stringByAppendingString:#" \n"];
}
Call this method like this..
[YOUR_LABEL alignTop];
Either You set no of Lines to be 0 like
[yourLabelObject setNumberOfLines:0];
[yourLabelObject sizeToFit];
Or
You can find the height of label at run time depending upon textString length.
Following method will return you the size(height & width) of label for length of text string.
here width is fixed and only height will change :
- (CGSize) calculateLabelHeightWith:(CGFloat)width text:(NSString*)textString andFont:(UIFont *)txtFont
{
CGSize maximumSize = CGSizeMake(width, 9999);
CGSize size = [textString sizeWithFont:txtFont
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
Yo need to calculate frame of label each time when u r doing to set text and set frame of label
I hope this will helps you.
Might sound a bit silly but this is my approach: For any field that needs to be top aligned I fill the text up with multiple "\n"s. This causes the text to be automatically top aligned. Pretty much the same as Mehul's method above.
http://i.stack.imgur.com/8u5q4.png

Stretch width of UILabel according to width of containing text with background image

I have the following problem - I need to create two UI labels along side one another as in the screen shot below -
The UI label containing the special offers text is dynamic and needs to adjust to the width of the containing text and also if possible display the slanted orange background with the relevant padding -
I'm predominantly a Front-end dev - so with CSS i'd use a long background image that aligns to the right of the label and pad accordingly - but I have no idea how to approach this in objective C - can anyone offer any advice?
This is not a drop-in solution, but perhaps helps you find it (assuming you don't use Auto Layout):
You need a UIImageView for the background and a UILabel for the text
Use a tileable/strechable image (probably with cap insets) for the background (see [UIImage resizableImageWithCapInsets:resizingMode:])
Set the text on your label
Call [label sizeToFit] to resize the label to exactly fit the contained text
Resize the image view depending on the label's size (e.g. imageView.frame = CGRectInset(label.frame, -10, -10), which would make your image view 10pt larger than the label on all sides).
With Auto Layout you'd just define the appropriate constraints between the label and the image view and rely on the label's "intrinsic content size" - should be quite easy.
You could always shrink the text when it gets to long for the label, go into your view controller, click on the label that you would like to shrink when the text gets too long, then, go down to Autoshrink under Label in the attributes inspector. Change it from fixed font size to minimum font size, then I recommend putting 6 to 8 as the lowest font size. This is going to be the LOWEST font size though, so if XCode can make the label fit while making the font size 9, yet the lowest is 7, it will do it.
Or, you could get the length of the string with
int *stringLength = [myString length]
which counts spaces too, then, change the orange square with
orangeBoxImageView.frame = CGRectMake(0, 0, resizedWidth, resizedHeight);
so you could do something like this with your code:
if(stringLength == 15{
orangeBoxImageView.frame = CGRectMake(0, 0, 15, 5);
}
I hope this could help you
CGFloat rect = [YOURSTRING
boundingRectWithSize: (CGSize){ labelWidth, CGFLOAT_MAX }
options: NSStringDrawingUsesLineFragmentOrigin
attributes: #{ NSFontAttributeName : labelFont };
context: nil];
This assumes numberOfLines = 0.
What you want to use is UILabel -sizeToFit inherited from UIView. You should be able to figure out the rest from there.
Find the size of the containing text with
[text sizeWithFont:font constrainedToSize:CGSizeMake(width, height) lineBreakMode:NSLineBreakModexx]
Once you have that, you can manipulate the size of the label accordingly.
Hope this helps
For the background I'd create a subclass of UILabel that overrides drawInRect:, such as this example that simply draws a rectangle with the size of the label
-(void) drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [UIColor redColor].CGColor);
CGContextFillRect(context, rect);
}
Rather than drawing a rectangle, you should create a path with the shape that you need and fill that. See here for more info:
http://weblog.invasivecode.com/core-graphics
I don't know how you've created your label, but if it's done properly with autolayout it'll have the correct width automagically. See here:
http://www.raywenderlich.com/20881/
Tim

Vertically Align UILabel text with constraints and no wrap (auto layout, single line)

So I have my view setup in IB such that this text label aligns with the top of the thumbnail via constraints.
However as we know, you can't vertically align text in a UILabel. My text updates the font size based on the length of the content. Full size text looks great, while small text is significantly lower on the view.
The existing solution involves either calling sizeToFit or updating the frame of the uilabel to match the height of the text. Unfortunately the latter (albeit ugly) solution doesn't play well with constraints where you aren't supposed to update the frame. The former solution basically doesn't work when you need to have the text autoshrink until it truncates. (So it doesn't work with a restricted number of lines and autoshrink).
Now as to why the intrinsic size (height) of the uilabel doesn't update like the width does when it's set to it's natural size via "Size to fit content" is beyond me. Seems like it definitely should, but it doesn't.
So I'm left looking for alternative solutions. As far as I can see, you might have to set a height constraint on the label, and adjust the height constant after calculating the height of the text. Anyone have a good solution?
This problem is a real PITA to solve. It doesn't help that the API's that work are deprecated in iOS7, or that the iOS7 replacement API's are broken. Blah!
Your solution is nice, however it uses a deprecated API (sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:), and it's not very well encapsulated - you need to copy this code around to any cells or views where you want this behavior. On the plus side it's fairly efficient! One bug may be that the label hasn't been laid out yet when you do your calculation, but you perform your calculation based on its width.
I propose that you encapsulate this behavior in a UILabel subclass. By placing the sizing calculation in an overridden intrinsicContentSize method the label will auto-size itself. I wrote the following, which incorporates your code that will execute on iOS6, and my version using non-deprecated API's for iOS7 or better:
#implementation TSAutoHeightLabel
- (CGSize) intrinsicContentSize
{
NSAssert( self.baselineAdjustment == UIBaselineAdjustmentAlignCenters, #"Please ensure you are using UIBaselineAdjustmentAlignCenters!" );
NSAssert( self.numberOfLines == 1, #"This is only for single-line labels!" );
CGSize intrinsicContentSize;
if ( [self.text respondsToSelector: #selector( boundingRectWithSize:options:attributes:context: )] )
{
NSStringDrawingContext* context = [NSStringDrawingContext new];
context.minimumScaleFactor = self.minimumScaleFactor;
CGSize inaccurateSize = [self.text boundingRectWithSize: CGSizeMake( self.bounds.size.width, CGFLOAT_MAX )
options: NSStringDrawingUsesLineFragmentOrigin
attributes: #{ NSFontAttributeName : self.font }
context: context].size;
CGSize accurateSize = [self.text sizeWithAttributes: #{ NSFontAttributeName : [UIFont fontWithName: self.font.fontName size: 12.0] } ];
CGFloat accurateHeight = accurateSize.height * inaccurateSize.width / accurateSize.width;
intrinsicContentSize = CGSizeMake( inaccurateSize.width, accurateHeight);
}
else
{
CGFloat actualFontSize;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
[self.text sizeWithFont: self.font
minFontSize: self.minimumFontSize
actualFontSize: &actualFontSize
forWidth: self.frame.size.width
lineBreakMode: NSLineBreakByTruncatingTail];
#pragma GCC diagnostic pop
CGRect lineBox = CTFontGetBoundingBox((__bridge CTFontRef)([UIFont fontWithName: self.font.fontName size: actualFontSize]));
intrinsicContentSize = lineBox.size;
}
return intrinsicContentSize;
}
#end
This implementation isn't perfect. I had to ensure using baselineAdjustment == UIBaselineAdjustmentAlignCenters, and I'm not 100% certain I understand why. And I'm not happy with the hoops I had to jump through to get an accurate text height. There's also a few pixel difference between what my calculation produces, and yours. Feel free to play with it and adjust as necessary :)
The boundingRectWithSize:options:attributes:context API seems pretty broken to me. While it (mostly!) correctly constrains the text to the input size, it doesn't calculate the correct height! The height it returns is based on the line-height of the supplied font, even if a scaling is in play. My guess is this is why UILabel doesn't have this behavior by default? My workaround is to calculate an unconstrained size where both the height and width are accurate, then use the ratio between the constrained and unconstrained widths to calculate the accurate height for the constrained size. What a PITA. There are lots of complaints in the Apple dev forums and here on SO that point out that this API has a number of issues like this.
So I found a workaround. It's a little dicey, but it works.
So what I did was add a height constraint to my line of text in IB, and grab a reference to that in my view.
Then in layoutSubviews, I update my constraint height based on the size of the font, which I have to calculate:
- (void)layoutSubviews {
if (self.titleLabel.text) {
CGFloat actualFontSize;
CGSize titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font minFontSize:9.0 actualFontSize:&actualFontSize forWidth:self.titleLabel.frame.size.width lineBreakMode:NSLineBreakByTruncatingTail];
CGRect lineBox = CTFontGetBoundingBox((__bridge CTFontRef)([UIFont fontWithName:#"ProximaNova-Regular" size:actualFontSize]));
self.titleHeightConstraint.constant = lineBox.size.height;
}
[super layoutSubviews];
}
At first I was just setting it to the actual font size, but even with an adjustment (*1.2) it was still clipping the smaller font sizes. The key was using CTFontGetBoundingBox with the font size determined from my calculation.
This is pretty unfortunate, and I'm hoping there's a better way. Perhaps I should switch to wrapping.
TomSwift thanks for your answer, i really struggled with this issue.
If someone is still getting weird behaviour, i had to change:
intrinsicContentSize = CGSizeMake( inaccurateSize.width, accurateHeight);
to
intrinsicContentSize = CGSizeMake( inaccurateSize.width, accurateHeight * 2);
then it worked like charm.
What you're looking for is these two lines of code.
myLabel.numberOfLines = 0;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
and you will also find this in the Attributes Inspector under "Line Breaks" and "Lines".

determining UIFont's pointSize for a specific CGSize

I have a specific CGSize of a UILabel, where I cannot expand the frame of UILabel and since it is a multi-line UILabels adjustsFontSizeToFitWidth method does not work.
So I figured I should create such function which looked like;
- (CGFloat)fontSizeWithText:(NSString*)text andFont:(UIFont*)font constrainedSize:(CGSize)size LBM:(UILineBreakMode)LBM
{
// check if text fits to label
CGSize labelSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(size.width, 9999) lineBreakMode:LBM];
// if not, decrease font size until it fits to the given size
while (labelSize.height > size.height) {
font = [UIFont fontWithName:font.fontName size:font.pointSize - 0.5];
labelSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(size.width, 9999) lineBreakMode:LBM];
}
return font.pointSize;
}
Usage:
// fit detail label by arranging font's size
CGFloat fontSize = [self fontSizeWithText:self.titleLabel.text andFont:self.titleLabel.font constrainedSize:self.titleLabel.frame.size LBM:self.titleLabel.lineBreakMode];
self.titleLabel.font = [UIFont fontWithName:self.titleLabel.font.fontName size:fontSize];
But with this method, I see that some of my texts doesn't fit to the UILabel s frame and gets truncated. There must be something I am missing. Any help for the code or any other suggestions on resolving "fitting a text with given font to a specific multi-line UILabel" would be great.
First lets quickly take a look at the problem as a whole. You're trying to fit text into a predefined frame and adjust the font size. This generally will not work terribly well, as you will quickly hit sizes FAR too small to read, even on a retina display. You should adjust the frame of your label to accommodate the excess text (where possible. Sometimes, truncation is the only option.)
Now that, that is out of the way, lets take a look at adjusting the font size. Despite not recommending it, I will still explain how best to go about it.
Important, this code is untested, and will more than likely require some tweaks, but that can be an exercise of the reader.
So the first thing we need to know is the height of a single line. Now, we have the height of the label, and the number of lines it can display, so we can determine this by simply dividing the label height by the number of lines.
CGFloat optimalLineHeight = CGRectGetHeight(label.frame) / label.numberOfLines;
You may have noticed that this may return lines taller than are actually needed. You will be able to implement additional checks and constraints to deal with this. At current though, the font size will also be able to grow, and not just shrink.
Now, getting the optimal line height is just part of the story. We now need to optimise the font size. Here's some code:
CGFloat optimumFontSize = 0.0;
BOOL optimumFontSizeFound = NO;
do {
CGSize charSize = [#"M" sizeWithFont:[UIFont systemFontOfSize:optimumFontSize]
constrainedToSize:CGSizeMake(100, 9999)
lineBreakMode:0];
if ( CGSizeGetHeight(charSize) > optimalLineHeight ) {
optimumFontSizeFound = YES;
}
else {
optimumFontSize++;
}
} while ( !optimumFontSizeFound );
So what does this do? In this we keep track of the optimumFontSize so far. We start with the assumption of a font size of 0, and we see how tall a single character using that font size is. If that height is greater than the optimal line height previously calculated, then the previous height is the optimal one. If not, we increase the size and repeat until we do find the optimal one.
There are still a lot of issues to overcome in this to make it work perfectly in all situations. This should ensure that you don't get visible vertical clipping of characters, but it can't ensure that all the text content will display in the frame. To do that you'll need to be more intelligent in how you determine the number of lines required, but again I'll leave that as an exercise of the reader.
Hope this helps you towards your goal.

Resources