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.
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;
I need one help i don't know how to implement bullet in UILabel text. I search lots of links but couldn't find the solution. Please help me. Waiting for a solution.
Thanks in advance.
Use the Unicode code point for the bullet character in your string?
Try This:
myLabel.text = #"\u2022 This is a list item!";
You should take a look at AliSoftware's OHAttributedLabel. It is a subclass of UILabel that draws an NSAttributedString
try this:
#import "OHAttributedLabel.h"
#import "NSAttributedString+Attributes.h"
OHAttributedLabel *myLabel=[[OHAttributedLabel alloc]init];
[myLabel setFrame:CGRectMake(0, 0, 70, 20)];
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:#"\u2022 list item!!!"];
[attrStr setTextColor:[UIColor redColor]];
[attrStr setTextColor:[UIColor blackColor] range:NSMakeRange(0,1)];
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];
if you want to add just bullet than use this:
myLabel.text = #"\u2022 list item!!!!!!!!!";
List of more unicode
hi #Anamika Try this also
myLabel.text = #"\u25A0 This is a list item!";
and for more shapes you can chage the value from here :
http://www.alanwood.net/unicode/geometric_shapes.html
If formatting your text with bullets is a concern, I found this Stack Overflow post helpful. They show you how to create and align paragraphs containing bullets using storyboard.
Cmd+K on a mac will insert a bullet character.
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...
i have to add custom font file .Name of file if #"sample font.ttf".I do following steps.
Drop the file (sample font.ttf) into your project. Open up your Info.plist file, create a key called UIAppFonts and make it an array. Add the filename of the font as a value
But it not working ..I thought the reason may by space in filename.when i get this file it was a compressed with filename #"sample_font.ttf".when i decompress it ,it removes #"_" and get "sample font.ttf"
Then i install it in font book .the name in window is first word file that is "sample"
I try with various way
UIFont *font = [UIFont fontWithName:#"sample font" size:14];
UIFont *font = [UIFont fontWithName:#"sample" size:14];
UIFont *font = [UIFont fontWithName:#"samplefont" size:14];
UIFont *font = [UIFont fontWithName:#"samplefont-Bold" size:14];
but not working. what exact font name should have to given. ios is 5.0 .Plz help me.
Just double click the font file and install it and then it will open Font book
go to Preview menu->>Show font info
There you can see the name of the font and use that name in UIFont *font = [UIFont fontWithName:#"nameAsSeenInShowFonts" size:14];
Note: The font file name and font name can be different. So in your .plist you use font file name and in your code you use font name
if you try in Ios5 I think you forgot to include your font file in TestApp target membershipI
Except all these, you can use an alternet option for custom fonts. I have implemented this concept jst before some days.
First download this. This is FontLabel. Drop it in your project.
Note that If u want to use fonts for label than only this will help u. If so, than u can use FontLabel object instead of ur label object with same behavior.
For exa.
FontLabel *label;
label = [[FontLabel alloc] initWithFrame:CGRectMake(75, 100, 104, 54) fontName:#"Script MT Bold" pointSize:30.0f];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = nil;
[label sizeToFit];
label.opaque = NO;
You can treat label object same as ur UILabel object. FontLabel is subclass of UILabel..
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.