I have a UIlabel view which allow to show two lines of strings. But in my case, there is one long word only. Whatever I set the line break mode to UILineBreakModeTailTruncation or UILineBreakModeWordWrap, it always break the word into two lines. Like this:
"xxxxxx
xx"
I would like to truncate it in the first line, like this:
"xxxx..."
Is there any way to implement that.
In most of the cases, it should allow to show two lines of words.
Additional edit:
Take following image as an example. The top two labels are what I expected: one long word can be truncated in one line; multiple short words can be show in two lines.
The bottom label is currently happened.
In order to do what you're asking you need to find out if there is only one word. If there is, set the number of lines to 1 and the auto-shrink should fix things. If there is not, then set the number of lines to 0.
e.g.
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:12.0];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.8;
label.text = NSLocalizedString(#"Information", #"E: 'Information'");
NSUInteger words = [label.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].count;
label.numberOfLines = (words == 1) ? 1 : 0;
Swift 3.0
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 12)
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.8
label.text = NSLocalizedString("Information", comment: "E: 'Information'")
let words = label.text?.components(separatedBy: NSCharacterSet.whitespacesAndNewlines).count
label.numberOfLines = words == 1 ? 1 : 0
Dont do anything
set number of line 1
no break mode
set font size and min font size same
see the image
Set the number of line is one or don't set the number of lines by default is 1. And line break mode would be UILineBreakModeTailTruncation... and specially check the width of your label.... It should not be bigger use as following ...
[lbl setFrame:CGRectMake:(10,10,50,30)];
[lbl setLineBreakMode:UILineBreakModeTailTruncation];
[lbl setNumberOfLines:1];
May this will help you ...
By making the label more wider you can solve this issue.
[lbl setFrame:CGRectMake:(xPosition,yPosition,heightValue,21)];
[lbl setNumberOfLines:1];
where
xPosition & yPosition values are values that you set for your label.
heightValue is the appropriate value which will align your label in the view properly.
go on...
Related
I have to show multiple line in UILabel (If text is large). Below is my code. I am using separate properties for different iOS versions. Please help me out..
labelLocation.numberOfLines=2;
labelLocation.font=[UIFont systemFontOfSize:25];
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=6) {
labelLocation.lineBreakMode=NSLineBreakByTruncatingTail;
labelLocation.minimumScaleFactor=10.0/[UIFont labelFontSize];
}else{
labelLocation.lineBreakMode=UILineBreakModeTailTruncation;
labelLocation.minimumFontSize=10;
}
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
these two line together works
labelLocation.numberOfLines=0;
labelLocation.lineBreakMode = NSLineBreakByWordWrapping;
you can set
yourlabelname.lineBreakMode = NSLineBreakByWordWrapping;
yourlabelname.numberOfLines = give how many lines you want for your label(e.g.2,3,etc...)
and check if your outlet is set properly.
Try this labelLocation.numberOfLines=0;
I suppose, that your label has to small height. Two lines in systemFontOfSize 25 need height about 60.
If label is to small, system doesn't wrap line.
change ur code to this
labelLocation.numberOfLines=0;
labelLocation.font=[UIFont systemFontOfSize:40];
labelLocation.lineBreakMode=NSLineBreakModeWordWrap;
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
I would personally recommend you to calculate the height required to show the text and then show it onto the label...never hard code text display components such as UITextView and UILable.
NSString *str = #"This is to test the lable for the auto increment of height. This is only a test. The real data is something different.";
`UIFont * myFont = [UIFont fontWithName:#"Arial" size:12];//specify your font details here
//then calculate the required height for the above text.
CGSize lableSiZE = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(240, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
//initialize your label based on the height you got from the above..you can put whatever width you prefer...
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lableSiZE.width, lableSiZE.height)];
myLable.text = str;
myLable.numberOfLines=0;
myLable.font=[UIFont fontWithName:#"Arial" size:12];
//myLable.backgroundColor=[UIColor redColor];
myLable.lineBreakMode = NSLineBreakByWordWrapping;
Any way to have multiple lines of text in UILabel like in the UITextView?
I dont wish to use more than 1 label in the view.
Just use this code in your Program
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
Yes this can be done by setting the numerOfLines property to the number of lines you want to display. Or set it to 0 if you just want to be able to add as many lines as you want.
You can check out this answer which has everything you want
You can also set Properties like numberoflines and linebreakMode
have look to documentation as well
Use the numberOfLines property of UILabel and assign 0 for multiple lines.
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
set the lineBreakMode and numberOfLines property of UILabel and set the frame as see the multiple lines.
yourLabel.lineBreakMode = UILineBreakModeWordWrap;
yourLabel.numberOfLines = 0;
yourLabel.frame = CGRectMake(0,0,320,150);
yes,this is possible in .xib file select your label you found Lines option set value that you want to
i.e.
line 3
if you creating label programmatic than
lbl.numberOfLines = 0;
CGRect currentFrame = myLabel.frame;
CGSize max = CGSizeMake(lbl.frame.size.width, 500);
CGSize expected = [myString sizeWithFont:lbl.font constrainedToSize:max lineBreakMode:lbl.lineBreakMode];
currentFrame.size.height = expected.height;
lbl.frame = currentFrame;
This question already has answers here:
Vertically align text to top within a UILabel
(51 answers)
Closed 8 years ago.
On iOS 5.
I have a UILabel element that is originally placed in a nib. I want the x placement to stay fixed. I want it to take either 1 line, or 2 lines. If more than two lines, it should use the line break setting to show the ellipsis.
I use numberOfLines property and -sizeToFit
If I set the UILabel's numberOfLines property to 0, it will correctly see that for some text, there is not enough room and will wrap it to a second line after calling -sizeToFit but in the rare case that the line is long enough to stretch to 3 lines, I get three lines, which I don't want. If I set numberOfLines property to 2, it actually stretches the whole thing out onto one line and elongates my initial frame set in the nib to be much wider.
CGRect titleFrame = [[self titleLabel] frame];
[[self titleLabel] setNumberOfLines:0];
[[self titleLabel] setText:newProductTitleText];
[[self titleLabel] sizeToFit];
CGRect newTitleFrame = [[self titleLabel] frame];
The CGRect are just there for me to be able to calculate things after the fact. So setting numberOfLines to 0 works, and will not change the original origin.x in the frame, and will break long text into multiple lines, but will not constrain it to 2 lines. Setting numberOfLines property to 2, which, when I read the Apple docs
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
It seems I should be able set this to two and still have it work. I would expect sizeToFit to expand in a positive X and Y direction when expanding to fit all the text but it is expanding in a negative X direction when numberOfLines is set to other than 0.
ETA: the "Autosize" struts are set to upper and left to fix it at a min x,y.
Thanks for any insight.
I had a similar problem where -[UILabel sizeToFit] was not respecting the max width I set when numberOfLines was set to 2. Here's how I solved that problem:
CGFloat titleMaxWidth = 200;
CGFloat titleMinHeight = 30;
CGFloat titleMaxHeight = 40;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, titleMaxWidth, titleMaxHeight)]; // alternatively, you could do this in a nib
titleLabel.numberOfLines = 0;
titleLabel.text = #"The title label will be sized appropriately with this technique.";
titleLabel.font = [UIFont boldSystemFontOfSize:16];
[titleLabel sizeToFit];
titleLabel.numberOfLines = 2;
if (titleLabel.height > titleMaxHeight)
{
titleLabel.height = titleMaxHeight;
}
else if (titleLabel.height < titleMinHeight)
{
titleLabel.height = titleMinHeight;
}
As you can see, I also wanted a minimum height for my label, as -sizeToFit often makes the label really small, but you could disregard that code if you don't care about a minimum height. The "magic number" of 40 for the titleMaxHeight comes from experimentation and finding out that a 2 line label with this font really only needs 40px. In this code, -sizeToFit is mainly used to keep the text within the width and determine whether the initial height of 40 can be reduced when we have a short string of text.
I used the UIFont property lineHeight:
CGFloat labelHeight = label.font.lineHeight*label.numberOfLines;
i Have a large String and i want to display that in UILabel,but that displays only half of that... how can i got full string even if it 2 lines..?
The code i am used :
label.text =#"Kellogg's® All-Bran® Bran Buds® cereal";
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:label];
it displays only Kellogg's® All-Bran.
Thank you,
Anand
where u r declaring label use this
UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(50, 5, 250, 20)];
or if u do not want to do this then do this
lable.frame=CGRectMake(50, 5, 250, 20);
Another approach mich more dynamic in nature could be by using
CGSize expTSize = [yourLabel.text sizeWithFont:yourLabel.font];
You can fix the width upto some value and then divide the width of the above by your fixed width that will give you the number of lines as well. Hence finally you can use the above size variable to reset the frame of the label.
Hope this also works for you, if it does please communicate.
How to let a label show multi-line strings?
e.g. The given string is #"HelloA\nHelloB\nHelloC\n".
How to show it like:
#"HelloA"
#"HelloB"
#"HelloC"
I find some properties to customize UILabel:
label.numberOfLines = 3;
label.adjustsFontSizetoFitWidth = YES;
label.minimumFontSize = 8.0f;
And '\n' can be used as tail truncation mark.