I want a wordwrapping for string to just wanted for the dynamic height calculations purpose, because in UILabel NSLineBreakByWordWrapping, counting string height for dynamic purpose is easy to adjust UILabel height please can any buddy let me know the wordwrapping in string ?
CGSize size = CGSizeZero;
NSString *carNameString = cars.name;
carNameString = [carNameString stringByReplacingOccurrencesOfString:#"\n" withString:#"\n\n"];
CGRect f1 = [carNameString boundingRectWithSize:CGSizeMake([[UIScreen mainScreen]bounds].size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:[UIFont fontWithName:#"Copperplate" size:20] } context:nil];
size = CGSizeMake(f1.size.width, f1.size.height + 1);
Note: I am using iOS Version 6.3
Edit:
Here is the UILabel Code:
[carLabel setBackgroundColor:[UIColor clearColor]];
[carLabel setText:carNameString];
[carLabel setAdjustsFontSizeToFitWidth:YES];
[carLabel setLineBreakMode:NSLineBreakByWordWrapping];
[carLabel setNumberOfLines:0];
[carLabel setTextColor:[UIColor whiteColor]];
You don't seem to use the size you calculated from the bounding rectangle.
Also, I'd advise you use the same font for your label than you used to calculate the bounding rectangle instead of:
[label setAdjustsFontSizeToFitWidth:YES];
This code should work:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
label.numberOfLines = 0;
label.font = [UIFont fontWithName:#"Copperplate" size:20];
label.text = carNameString;
However, your code doesn't provide the initialization of your UILabel and you don't let us know how you set the frame of your label.
If you wish to use it with auto layout, you would have to set translatesAutoresizingMaskIntoConstraints to NO and to use the size calculated from the bounding rect to set the size of your container view.
Here's a sample based on your code describing it:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[self.view addSubview:containerView];
UILabel *label = [UILabel new];
label.translatesAutoresizingMaskIntoConstraints = NO;
[containerView addSubview:label];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[label]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(label)]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[label]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(label)]];
label.numberOfLines = 0;
label.font = [UIFont fontWithName:#"Copperplate" size:20];
label.text = carNameString;
Related
I want to make a game title but I can't seem to make the font larger. Here's what I have. I opted to not use CGRectMake initializer setting because I wanted to just set the x and y positions, not the size of the label (which I presumed would've been dictated by the size: portion of the UIFont method below). The 100.0 is no different from size 40.0.
UILabel *title = [[UILabel alloc] init];
CGRect frame = title.frame;
frame.origin.y = 30;
frame.origin.x = 100;
title.frame= frame;
title.text = #"TWINSTONES";
title.backgroundColor = [UIColor clearColor];
title.font = [UIFont fontWithName:#"TWINSTONES" size:100.0];
[title setTextColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
title.numberOfLines = 0;
[title sizeToFit];
[self.view addSubview:title];
I have a label that I'm creating and displaying programmatically. It can be 1 or more lines. I want to the label to be truncated at the end if it's too long. When the label is > 1 line long the following code works fine. Create a blank project and drop this into viewDidLoad to play along at home. Any iOS or tvOS project should do.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.backgroundColor = [UIColor blueColor];
[self.view addSubview:label];
NSDictionary *attributes = #{NSFontAttributeName:[UIFont systemFontOfSize:26.0]};
label.attributedText = [[NSAttributedString alloc] initWithString:#"The rain in Spain falls mainly on the plain." attributes:attributes];
CGSize maxLabelSize = CGSizeMake(200, CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maxLabelSize];
NSLog(#"requiredSize: %#", NSStringFromCGSize(requiredSize));
label.frame = CGRectMake(50.0, 50.0, requiredSize.width, requiredSize.height);
However, if I change numberOfLines to 1 then sizeThatFits returns a size with a width wide enough to fit the entire string even though it's bigger than the width of maxLabelSize.
I can work around this by checking to see if requiredSize.width is greater than maxLabelSize.width, and adjusting appropriately, but I'd like to know why sizeThatFits behaves differently with a 1-line label than with a multi-line label. I would expect a size no greater than 200 with the height the same as the attributed string's line height.
I have no idea why sizeThatFits doesn't work, but another method textRectForBounds:limitedToNumberOfLines: does the trick. Something like
label.numberOfLines = 0;
CGSize requiredSize = [label textRectForBounds:CGRectMake(0, 0, 200, CGFLOAT_MAX) limitedToNumberOfLines:1].size;
UILabel * commlbl;
commlbl=[[UILabel alloc]initWithFrame:CGRectMake(10, commlbl1.bounds.size.height+50, commscroll.bounds.size.width-25, commscroll.bounds.size.height+70)];
[commlbl setFont:[UIFont fontWithName:#"OpenSans-Regular" size:16]];
[commlbl setTextColor:[UIColor whiteColor]];
[commlbl setTextAlignment:NSTextAlignmentCenter];
commlbl.lineBreakMode = NSLineBreakByWordWrapping;
commlbl.numberOfLines = 0;
commlbl.text = [USER_DFT GetUserDefault:#"msgString"];
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [commlbl.text sizeWithFont:commlbl.font constrainedToSize:maximumLabelSize lineBreakMode:commlbl.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = commlbl.frame;
newFrame.size.height = expectedLabelSize.height;
commlbl.frame = newFrame;
UILabel is created like this:
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 10, 268, 44)];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor yellowColor];
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
Then to update the label I do this:
label.text = someText;
CGRect frame = label.frame;
NSDictionary *attributes = #{NSFontAttributeName: label.font};
CGRect rect = [someText boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
frame.size.height = rect.size.height;
label.frame = frame;
This works fine with the label only has one line of text, but I have a maximum of two lines. When the 2nd line gets truncated, extra bottom padding gets added to the label, which makes it impossible to position the label below it (the red label in the images):
Why does this happen? If it's a bug, is there a workaround?
Try something like this:
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
UILabel *anotherLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myLabel.frame.size.width, MAXFLOAT)];
[anotherLabel setAttributedText:attrString];
[anotherLabel setNumberOfLines:2];
[anotherLabel setText:NSTextAlignmentLeft];
[anotherLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[anotherLabel sizeToFit];
[myLabel setAttributedText:attrString];
[myLabel setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, [anotherLabel frame].size.height)];
i used loop to create dynamic UIView. But i could not create UIView according the size of UILabel kindly help me as i could do further proceed.. need your help.. i will appreciate
for(NSString *item in myArray)
{
length = item.length;
self.custom=[[CustomView alloc]init];
self.custom.Label = [[UILabel alloc]initWithFrame:CGRectMake(40, yAxis, 100+item.length, 44)];
[self.custom setFrame:self.custom.Label.frame];
[self.custom sizeToFit];
yAxis=yAxis+50;
self.custom.tag=200+a;
a++;
[newViews addObject:self.custom];
length = item.length;
self.custom.Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5,length+20,40)];
self.custom.button=[[UIButton alloc]initWithFrame:CGRectMake(85,10,12,10)];
[self.custom.Label setText:item];
// Tell the label to use an unlimited number of lines
[self.custom.Label setNumberOfLines:1];
[self.custom.Label sizeToFit];
self.custom.Label.textAlignment = UITextAlignmentCenter;
![this is image i have created uiview according the array values where uilabel more width than uiview so i need to set uiview according the uilabel][1]
UIImage *btnImage = [UIImage imageNamed:#"button_droparrow.png"];
[self.custom.button setImage:btnImage forState:UIControlStateNormal];
[self.custom.button addTarget:self
action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchDown];
self.custom.button.tag=self.custom.button.tag+a;
self.custom.backgroundColor=[UIColor greenColor];
custom.Label.text=item;
custom.Label.backgroundColor = [UIColor yellowColor];
[self.custom addSubview:self.custom.button];
[self.custom addSubview:custom.Label];
// [self.custom addSubview:cv];
[self.view addSubview:self.custom];
}
You can get the size based of the UILabel content using the following code.
NSString * countString = aLbl.text;
CGSize s = [countString sizeWithFont:[UIFont fontWithName:#"Helvetica-Light" size:12] constrainedToSize:CGSizeMake(100, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
And alter the Size of any views using following code
CGRect rect = aView.frame;
rect.size.width = s.width;
rect.size.height = s.height;
aView.frame = rect;
try size to fit for label or view
[yourlabel sizeToFit];
or
[yourview sizeToFit];
Change setNumberOfLines:0 and run your app. I hope it helps you. And I request to you, if this is help you please vote me.
[self.custom.Label setNumberOfLines:0];
self.custom.Label.text = #"xyz.................";
[self.custom.Label sizeToFit];
I've read many questions on the topic but I can't seem to find what is wrong with my code:
UILabel *nameLabel = [[UILabel alloc] init];
[nameLabel setText: _nameString];
nameLabel.textAlignment = UITextAlignmentLeft;
nameLabel.contentMode = UIViewContentModeTop;
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.numberOfLines = 0;
nameLabel.font = [UIFont fontWithName:#"Verdana" size:14];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.textColor = [UIColor colorWithRed:0 green:0.282 blue:0.31 alpha:1];
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
CGSize maximumLabelSize = CGSizeMake(200.0f, 60.0f);
CGSize expectedLabelSize = [_nameString sizeWithFont:nameLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:nameLabel.lineBreakMode];
nameLabel.frame = CGRectMake(10, 10, expectedLabelSize.width, expectedLabelSize.height);
And although sometimes it does work (on larger texts) on texts like "Airplanes being the future" the expectedLabelSize returns height 18.0f and it cuts the sentence on the "Airplanes being the"
What am I doing wrong here?
I had the same problem once, that was because my label's width was smaller than the maximum Label's width wat I used to calculate the "expectedLabelSize".
Since you are using an autoresizingMask your label might be too small.