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);
Related
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.
I have a UIButton on which I call sizeToFit after settings its title. In cases where the title text is very short (probably < 20pt), the button is taking on a few points of padding on the left and right. For anything longer, the padding disappears. It is as if the button has an internal minimum width that is respected when sizeToFit is called. Does anyone know how to prevent this padding?
This works, Just change the string to whatever and and enjoy the resizing miracle.
NSString * helloKitty = #"I love cats";
UIButton * ss = [UIButton buttonWithType:UIButtonTypeCustom];
[ss setTitle:helloKitty forState:UIControlStateNormal];
[[ss titleLabel] setFont:[UIFont systemFontOfSize:14]];
[ss setBackgroundColor:[UIColor pink] forState:UIControlStateNormal];
CGSize strSizer = [helloKitty sizeWithAttributes:#{NSForegroundColorAttributeName:[UIColor blackColor],
NSFontAttributeName:[UIFont systemFontOfSize:14]}];
[ss setFrame:CGRectMake(100,200,strSizer.width, strSizer.height)];
[self.view addSubview:ss];
I have a UIButton in table cell. I want to place the button at the center of the cell. The text of the button is dynamic. This is what I have tried, but it's not coming at the center. It is coming towards the right of center.
UIButton *btnName = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGSize maximumLabelSize = CGSizeMake(self.frame.size.width, MAXFLOAT);
NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin;
NSDictionary *attr = #{NSFontAttributeName: [UIFont systemFontOfSize:15]};
CGRect labelBounds = [titleText boundingRectWithSize:maximumLabelSize
options:options
attributes:attr
context:nil];
CGFloat width = ceilf(labelBounds.size.width);
btnName.frame = CGRectMake(0.0, imgPic.frame.origin.y+imgPic.frame.size.height+20.0, width, 20.0);
btnName.center = CGPointMake(cell.contentView.center.x, imgPic.frame.origin.y+imgPic.frame.size.height+10.0);
[btnName setTitle:titleText forState:UIControlStateNormal];
[btnName setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[cell.contentView addSubview:btnName];
Just add below line in your code.
btnName.center = cell.center;
Your code looks fine, only you've to make these change,
You should center the button after you add it cell, and
Correct the center points
[cell.contentView addSubview:btnName];
btnName.center = CGPointMake(cell.frame.size.width/2.f, btnName.center.y);
Vladimir's answer saved my day. I used setAutoresizingMask and now it is coming absolutely fine.
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
I create a UITextView, add text to it, and put it in the view (with a container)
UITextView *lyricView = [[UITextView alloc] initWithFrame:screen];
lyricView.text = [NSString stringWithFormat:#"\n\n%#\n\n\n\n\n\n", lyrics];
[container addSubview:lyricView];
[self.view addSubview:container];
I then get the size of it for use with a button and add it to the UITextView
CGRect size = [lyrics boundingRectWithSize:CGSizeMake(lyricView.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[lyricView font]}
context:nil];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton setFrame:CGRectMake(56, size.size.height + 55, 208, 44)];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[lyricView addSubview:doneButton];
This works in most cases. This will respect \n line breaks (like I added in my stringWithFormat) but it will not respect word wraps automatically added by the text view. So if lyrics has a line that doesn't fit on the screen, the UITextView will wrap it (as it's supposed to), but size is now slightly shorter than it should be because it did not respect the text view wrap.
You can tell boundingRectWithSize to process the string in word-wrapping mode. You have to add an NSParagraphStyle attribute to the attributes parameter, with its lineBreakMode set to NSLineBreakByWordWrapping. So:
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
// ...whatever other attributes you need...
NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
[attr setObject:paraStyle forKey:NSParagraphStyleAttributeName];
then use attr as the attributes argument to boundingRectWithSize.
You can easily extend/generalise this technique to read other attributes including existing paragraph style attributes from whatever source makes sense.
Should use (NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) for options parameter.
Did some more research and ended up finding this.
CGSize textSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, FLT_MAX)];
CGFloat textHeight = textSize.height;
Hope this helps someone out there!