I have adjusted the UILabel font size according to the width in this way.
[btnPending.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:15]];
[btnPending setBackgroundColor:[UIColor clearColor]];
btnPending.titleLabel.adjustsFontSizeToFitWidth = YES;
btnPending.titleLabel.numberOfLines = 1;
btnPending.titleLabel.lineBreakMode = NSLineBreakByClipping;
[_vwTabBtnParent addSubview:btnPending];
And its working fine. But now I want to get the current font size of that UILabel. How can I do this in objective-c.
Please help me,
Thanks
Then to get the font name and size all you need is
NSString *fontName = self.label.font.fontName;
CGFloat fontSize = self.label.font.pointSize;
Try below method:
[label.text sizeWithFont:label.font
minFontSize:label.minimumFontSize
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
Note: Deprecated in iOS 7 with no alternative.
Related
Iam dynamically generating UIButton and when i set title name without space its showing correctly.
For eg:
NSString *myString=[[array valueForKey:#"optionName"] objectAtIndex:i];
NSLog(#"%#",myString); // myString=SampleTitle
[button setTitle:myString forState:UIControlStateNormal];
and when i set name with space its auto trimming UIButton title.
For eg:
NSString *myString=[[array valueForKey:#"optionName"] objectAtIndex:i];
NSLog(#"%#",myString); // myString=SampleTitle Test
[button setTitle:myString forState:UIControlStateNormal];
Its showing like this
HOw can i remove this trimming part? i need to show all characters properly.
and my UIButton size is calculating based on title
[button setTitle:myString forState:UIControlStateNormal];
CGSize stringsize = [#"Prolonged inspiration" sizeWithFont:[UIFont systemFontOfSize:14]];
//or whatever font you're using
[button setFrame:CGRectMake(x,y,stringsize.width+30,stringsize.height+20)];
The most likely cause of your issue is that you calculate the size of the text using a font that is likely different from the button's font. Base your calculation on the button's font as well as the button's text.
CGSize stringsize = [myString sizeWithFont:button.titleLabel.font];
As a side note, the sizeWithFont: method was deprecated back in iOS 7.0. Unless you are also supporting iOS 6, you should replace that method with the proper replacement.
Try this for iOS versions above iOS 7.0
CGSize textSize = { widthValue, CGFLOAT_MAX };
CGRect frame = [text boundingRectWithSize:textSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{ NSFontAttributeName:font }
context:nil];
size = CGSizeMake(frame.size.width, frame.size.height+1);
how to change font size of label without changing font family in xcode6 ?I am using following line of code:
lblMain.font = [UIFont systemFontOfSize:22];
Here's an example:
lblMain.font = [UIFont fontWithName:#"HelveticaNeue" size:22];
Or - you can even use the current font:
lblMain.font = [lblMain.font fontWithSize:22];
NSString *fontName = lblMain.font.fontName;
CGFloat fontSize = lblMain.font.pointSize;
[lblMain setFont:[UIFont fontWithName:fontName size:NEWSIZE]];
Use following code, if you just want to change font size keeping the same font family:
lblMain.font = [UIFont fontWithName:lblMain.font.fontName size:22.0f];
OR
You can also use this code:
lblMain.font = [lblMain.font fontWithSize:22.0f];
[lblMain setFont:[UIFont fontWithName:#"HelveticaNeue" size:17]];
I'm trying to vertically center a UITextView using the appropriate height retrieved from sizeThatFits. There are a plethora of other answers that suggest this is the most appropriate way of calculating this.
(Note that I've tried it both with plain and attributed strings, and both exert the same behavior).
No matter what I try (even using attributed strings and setting the font size or line height to something bigger or smaller) it always only shows a certain truncated number of characters of text (in this case, 3 lines, and it's always exactly the same). What am I missing?
_textView.text = [_collectionDescription lowercaseString];
_textView.font = [UIFont fontLight:22];
_textView.textColor = [UIColor whiteColor];
_textView.textAlignment = NSTextAlignmentCenter;
_constraintTextViewHeight.constant = ceilf([_textView sizeThatFits:CGSizeMake(_textView.frame.size.width, FLT_MAX)].height);
[_textView setNeedsDisplay];
[_textView updateConstraints];
As always with AutoLayout, you have to do:
[_textInfoView layoutIfNeeded];
(don't even need the setNeedsDisplay at all).
So, fully working:
_textView.text = [_collectionDescription lowercaseString];
_textView.font = [UIFont fontLight:22];
_textView.textColor = [UIColor whiteColor];
_textView.textAlignment = NSTextAlignmentCenter;
_constraintTextViewHeight.constant = ceilf([_textView sizeThatFits:CGSizeMake(_textView.frame.size.width, FLT_MAX)].height);
[_textView layoutIfNeeded];
[_textView updateConstraints];
Instead of using sizeThatFits: method, you may use following method:
well you can try this:
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:14]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
NSString *text = _textView.text;
CGRect rect = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
_constraintTextViewHeight.constant = CGRectGetHeight(rect)
This method also considers line breaks in string text
This is what I'm doing. I'm not dynamically creating the label, just picking from the object library.it is working only if I statically increase the height of the label, but I doesn't know at runtime
[self.lblDescription setText:#"This is going to be a test description but a very big description that should increase in height"];
[self.lblDescription sizeToFit];
The label is not going on the next line. But instead it is clipping it as soon as the left of the label reaches the right of the screen
Based on the answers and I tried the following but didn't work:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.lblDescription setText:#"This is going to be a test description but a very big description that should increase in size"];
self.lblDescription.numberOfLines = 0;
[self.lblDescription sizeToFit];
}
I also tried setting -
[self.lblDescription setLineBreakMode:NSLineBreakByWordWrapping];
before calling [self.lblDescription sizeToFit]; but didn't work
Setting adjustsFontSizeToFitWidth property to true did it for me.
Try,
self.lblDescription.adjustsFontSizeToFitWidth = true
(by default it is set to false)
Check for the numberOfLines property
self.lblDescription.numberOfLines = 0;
EDIT : Need to change the height as well (By the comment says) .
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica Neue" size:19], NSFontAttributeName,
nil];
CGRect expectedLabelFrame = [text boundingRectWithSize:CGSizeMake(571, 500)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
// Adjust the yourLabel the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelFrame.size.height;
yourLabel.frame = newFrame;
I've found that when using sizeToFit with a UILabel, in InterfaceBuilder you need to change the Autoshrink property from 'Fixed Font Size' to 'Minimum Font Size'. I usually then set its value to 0.5 to be sure its working properly.
I've tried to search online, but haven't found a clear answer for my issue so I've come to ask for your expert advice. I have a view with 2 labels on it. The first label is used to show an abbreviation of a name, 2-3 letters. and the second label shows the full name.
The question that I have is if there was a way to dynamically size the label based on the font type, size, and string length given? I ask because I would like the second label close to the first without too much empty space between the two or without the first label overlapping the second.
The reason that this isn't all in one label is because the first label should have a bigger font and different color scheme then the second label.
Any advice is greatly appreciated.
You can calculate the size of in which your string will appear and then can set frame of your UILabel to that size see following code as a sample -
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
Update -
Use sizeWithAttributes: instead, which now takes an NSDictionary. Pass in the pair with key UITextAttributeFont and your font object like this:
CGSize size = [string sizeWithAttributes:
#{NSFontAttributeName:
[UIFont systemFontOfSize:17.0f]}];
Check Replacement for deprecated sizeWithFont: in iOS 7? for more details
This will also do the trick and will take into account attributed text
label.attributedText = attrString;
CGSize maximumLabelSize = CGSizeMake(187,CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maximumLabelSize];
CGRect labelFrame = label.frame;
labelFrame.size.height = requiredSize.height;
label.frame = labelFrame;
'sizeWithFont:' is deprecated: first deprecated in iOS 7.0.
so try sizeWithAttributes
- (void)viewDidLoad
{
[super viewDidLoad];
label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 300, 20)];
label.backgroundColor = [UIColor blueColor];
const CGFloat fontSize = 30;
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
UIColor *foregroundColor = [UIColor blackColor];
attrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName,
foregroundColor, NSForegroundColorAttributeName
, nil];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
initWithString:#"Test Text"
attributes:attrs];
[label setAttributedText:attributedText];
[self.view addSubview:label];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGSize expectedLabelSize = [label.text sizeWithAttributes:attrs]; // iOS 7 Code <--
CGRect newFrame = label.frame;
newFrame.size.width = expectedLabelSize.width;
label.frame = newFrame;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
initWithString:#"Longer Test Text"
attributes:attrs];
[label setAttributedText:attributedText];
}
As sizeWithFont: is deprecated in iOS 7, you need to use sizeWithAttributes (as maver explained here). In order to simplify code, you can add a method that you can reuse like this:
-(CGRect)rectForText:(NSString *)text
usingFont:(UIFont *)font
boundedBySize:(CGSize)maxSize
{
NSAttributedString *attrString =
[[NSAttributedString alloc] initWithString:text
attributes:#{ NSFontAttributeName:font}];
return [attrString boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
}
And make use of it
CGSize maximumLabelSize = CGSizeMake(280,9999);
UIFont *font = [UIFont systemFontOfSize:20];
CGRect titleRect = [self rectForText:post.title // <- your text here
usingFont:font
boundedBySize:maximumLabelSize];
In addition of Saurabh's answer. I had the same problem and you should add this line [yourLabel setNumberOfLines:0]; in order the whole text to be shown in X lines.