How to customize a mutiline label on iPhone? - iphone

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.

Related

Swift UITextView change font size if text is longer than two lines

How can I make a UITextView change its font size so that all of the text fits within a maximum of two lines? If the text fits one or two lines perfectly then I don't want the font size to change. But if the text is any longer, I need it to get smaller. Thanks.
I believe you have a great reason to use UITextView, and thus I think this can be little tricky and needs a lot of testing to see it actually works just as you expected.
What I would suggest is to calculate number of rows by:
let font = yourFont
let size = yourTextView.contentSize
let inset = yourTextView.textContainerInset
let rows = (size.height - inset.top - inset.bottom) / font.lineHeight
Get rows inside textView's delegate method, something like textView:shouldChangeTextIn:... for instance, and change font size according to this information.
Another suggestion is to make invisible UILabel with properties set like below:
label.font = yourFont
label.frame = CGRect(x: 0, y: 0, width: yourTextView.contentSize.width, height: yourTextView.contentSize.height)
label.numberOfLines = 2
label.adjustFontSizeToFitWidth = true
Then inside textView's delegate method, update label's text and
textView.font = label.font
I'm not sure either of these will actually work since I haven't tested them for myself, but I hope this may help you to set a start point for this problem.
Unless you have a specific need to be using a UITextView, I recommend using a UILabel. This code limits a UILabel to two lines and makes its text auto-shrink:
label.numberOfLines = 2
label.adjustsFontSizeToFitWidth = true

Add Multiple lines of text in UILabel

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;

UILabel with mutiple lines to truncate one long word

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...

problem in Text Alignment for label in iphone SDK

We have label with text in 2 lines. In second line the text is not aligning exactly to the left as in the first line. we had set property for the label as number of lines = 0.
for Ex:
label.text = #"This is the text for the label."
In Result We are getting this as
This is the text
for the label.
But we want this as
This is the text
for the label.
try setting the word wrap of button:
[label setNumberOfLines:0]
Edit:
just realised there's linebreak property of label, that should help
label.lineBreakMode = UILineBreakModeWordWrap;

another question about line break in UILabel

I'm struggling with line break in UILabel.
I'm generating xml in vb.net and then parse it in iPhone application. xml contains text which initially contains html tags such as , so I can and need to replace these tags with something to add a linebreak in iphone
How can I do it? I tried \n , \n\r, they a not working
Any help will be appreciated
Thank you
If you read a string from an XML file, the line break \n in this string will not work in UILabel tekst. The \n is not parsed to a line break.
Here is a little trick to solve this issue:
// correct next line \n in string from XML file
NSString *myNewLineStr = #"\n";
myLabelText = [myLabelText stringByReplacingOccurrencesOfString:#"\\n" withString:myNewLineStr];
myLabel.text = myLabelText;
So you have to replace the unparsed \n part in your string by a parsed \n in a hardcoded NSString.
Here are my other label settings:
myLabel.numberOfLines = 0;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor];
myLabel.font = [UIFont fontWithName:#"Helvetica Neue" size:14.0];
myLabel.textAlignment = UITextAlignmentCenter;
Most important is to set numberOfLines to 0 (= unlimited nr of lines in label).
No idea why Apple has chosen to not parse \n in strings read from XML?
Hope this helps,
Al
\n should work. Make sure you set numberOfLines to 0, and lineBreakMode to something that suits you.