UILabel text doesn't word wrap - iphone

I have a long text string (including \n newline characters) that I feed into a UILabel for display. The UILabel is dynamically set up to provide sufficient space for the text.
My code looks like this:
myText = [NSString stringWithFormat:#"%#some text: %# \n \n %#", myText, moreText1, moreText2];
NSLog(#"%#", myText);
myLabelSize = [vLabelText sizeWithFont:[UIFont fontWithName:#"Helvetica" size:(15.0)] constrainedToSize:cMaxLabelSize lineBreakMode:UILineBreakModeWordWrap];
UILabel *lBody = [[UILabel alloc] initWithFrame:CGRectMake(cFromLeft, vFromTop, vLabelSize.width, vLabelSize.height)];
lBody.font = [UIFont fontWithName:#"Helvetica" size:(15.0)];
lBody.lineBreakMode = UILineBreakModeWordWrap;
lBody.textAlignment = UITextAlignmentLeft;
lBody.backgroundColor = [UIColor cyanColor];
[myScrollView addSubview:lBody];
lBody.text = vLabelText;
My problem is that the text does not wrap, but truncates after the first line. The \n newlines are ignored.

Just found the problem, my numberOfLines was still at 1. After setting it to 0 it works fine.

May be it will be useful - since some lines are deprecated now
lBody.lineBreakMode = NSLineBreakByCharWrapping;
lBody.textAlignment = NSTextAlignmentLeft;
and of course - lBody.numberOfLines should be more than 1(1 is default value)

Related

single line appears as vertical-center aligned

I have a long text string that I feed into a UILabel for display. The UILabel is dynamically set up to provide sufficient space for the text
specialities1 =[[UILabel alloc]init];
[specialities1 setFrame:CGRectMake(124,218,size8.width, size8.height)];
specialities1.backgroundColor=[UIColor clearColor];**strong text**
specialities1.lineBreakMode=UILineBreakModeClip;
specialities1.font=[UIFont fontWithName:#"Helvetica-Bold" size:14];
specialities1.text=[NSString stringWithFormat:#"%# ",temp ];
specialities1.textAlignment=UITextAlignmentRight;
[specialities1 setNumberOfLines:0];
[specialities1 sizeToFit];
[testscroll addSubview:specialities1];
My problem is if text contains only single line then it appears as vertical-center aligned. That alignment is not matching with my respective label in front of it.
Use below code:
specialities1.lineBreakMode = UILineBreakModeWordWrap;
specialities1.textAlignment = UITextAlignmentLeft;
specialities1.textAlignment = UITextAlignmentLeft; // UITextAlignmentCenter
Choose what you need, depending on the situation you encounter.

spacing inside label's border

Friends,
i have created a UILabel with border (just like the image below).
I want to start my label after one/two lines left and then after finishing last line of label again one/two lines below.
Is there any way to get spacing inside label's border?
UILabel *cmntBoxlbl = [[UILabel alloc]initWithFrame:CGRectMake(58, 23, 250, 60)];
cmntBoxlbl.font=[UIFont fontWithName:#"Arial" size:12];
cmntBoxlbl.layer.borderColor = [UIColor darkGrayColor].CGColor;
cmntBoxlbl.layer.borderWidth = 1.0;
NSString *text = [NSString stringWithFormat:#"%#%#%#",#" ",[[self.DtlArray objectAtIndex:indexPath.row] objectForKey:#"comment"],#" "];
cmntBoxlbl.text = text;
cmntBoxlbl.textAlignment = UITextAlignmentCenter;
cmntBoxlbl.lineBreakMode = UILineBreakModeWordWrap;
[cmntBoxlbl setTextColor:[UIColor darkGrayColor]];
CGSize expectedLabelSize = [text sizeWithFont:cmntBoxlbl.font
constrainedToSize:cmntBoxlbl.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = cmntBoxlbl.frame;
newFrame.size.height = expectedLabelSize.height;
cmntBoxlbl.frame = newFrame;
cmntBoxlbl.numberOfLines = 0;
[cmntBoxlbl sizeToFit];
[cell addSubview:cmntBoxlbl];
Make current label(commentLabel) color is white. create another label with same content and a little smaller size,place it inside the boarder.Make padding as you wish
Have you tried this-
NSString *text = [NSString stringWithFormat:#"\n\n%#%#%#\n\n",#" ",[[self.DtlArray objectAtIndex:indexPath.row] objectForKey:#"comment"],#" "];
You may create a custom view and
- (void)drawRect:(CGRect)rect {
......;
[string drawInRect:rect withFont:font lineBreakMode:mode alignment:ali];
......;
}
hope that helps

iPhone/Objective - C - How to display the whole text of a long string in a UILabel

I have a very long string which is something like that below...
[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3],[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4]],[sqrt(sqrt(221)+15),sqrt(-sqrt(221)+15)]
How can i display the whole thing in a UILabel with more than one lines (0)?
Also, although there are no linebreaks ("\n") in the string, the result is displayed like below....
[[sqrt(221)-10,-sqrt(221)- 10],[11,11]],[[rootof([[474368400,2 37184200,- 125944810200,258530778000],[1, 0,- 472,884,42215]])/(sqrt(221)*78270
Any help?
PS I could upload an image but i am not allowed beacause i am a new user...
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
set the UILabel as a multiline
MyLbl.numberOfLines = xxx;
if you have not enough space for set the UIlabel then also set the textView As an not editable property:
Mytxt = [[UITextView alloc]initWithFrame:CGRectMake(xx.0, x.0, xx.0,xx.0)];
Mytxt.transform = transform;
Mytxt.adjustsFontSizeToFitWidth = YES;
Mytxt.textColor = [UIColor blackColor];
Mytxt.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[Mytxt setFont:[UIFont fontWithName:#"Arial" size:xx.0]];
Mytxt.backgroundColor = [UIColor clearColor];
Mytxt.delegate = self;
Mytxt.userInteractionEnabled=YES;
Mytxt.editable=NO;
i hope you get the answer.
You can use UITextView with auto size with non editable mode instead of UILabel (or)
CGSize labelSize = yourString sizeWithFont:cellFont
constrainedToSize:CGSizeMake(190.0f, MAXFLOAT); lineBreakMode:UILineBreakModeWordWrap];
This labelsize will dynamically define the label height you can assign to your label
try this code
NSString *yourString = #"[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])";
UILabel *lblNameDesc=[[UILabel alloc] initWithFrame:CGRectMake(55, 10, 250, 21)];
[lblNameDesc setNumberOfLines:0];
lblNameDesc.text=yourString;
[lblNameDesc setBackgroundColor:[UIColor clearColor]];
[lblNameDesc setTextColor:[UIColor colorWithRed:64.0/255.0 green:64.0/255.0 blue:64.0/255.0 alpha:1.0f]];
[self.view addSubview:lblNameDesc];
// [lblNameDesc release];
[lblNameDesc sizeToFit];
supoose your string is. make your fond and width what ever you want:-
NSString longString = #"dsjkghsdkjnfgvsdjgsl,v.........................";
CGSize maximumLabelSize = CGSizeMake(200,MAXFLOAT);
CGSize expectedLabelSize = [longString sizeWithFont:[UIFont fontWithName:#"Aial-BoldMT" size:15.0] constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, expectedLabelSize.height)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = longString ;
may this will help you
Try This...
textLabel.lineBreakMode = YES;
textLabel.numberOfLines = 10; // or many that you need...
You want UILineBreakModeCharacterWrap if you want your text to be evenly distributed along several lines (so it doesn't try to break at work boundaries).

UITextView breaks line automatically if string is shorter than 2 lines

I set up a UITextView that acts strange. If I set it's content to a long text it breaks it correctly at the boundaries however if the content is single lined it automatically breaks it before the last word even if it fits in one line. It created sort of a minimum of lines.
I wasn't able to find this issue posted on the web so I had to ask.
Here's the set up of the UITextView
Message * displayedMessage = [messages objectAtIndex:indexPath.row];
UIFont * messageFont = [UIFont fontWithName:#"Helvetica" size:13];
CGSize messageSize = [displayedMessage.content sizeWithFont:messageFont constrainedToSize:CGSizeMake(240.f, 3000.f) lineBreakMode:UILineBreakModeWordWrap];
UITextView * messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(46.f, 0.f, messageSize.width, messageSize.height + 25)];
messageTextView.backgroundColor = [UIColor clearColor];
messageTextView.font = messageFont;
messageTextView.editable = NO;
messageTextView.scrollEnabled = NO;
messageTextView.contentInset = UIEdgeInsetsZero;
messageTextView.textColor = [UIColor blackColor];
[messageTextView setDataDetectorTypes:UIDataDetectorTypeAll];
messageTextView.text = displayedMessage.content;
[cell addSubview:messageTextView];
I managed to solve it eventually i just had to define the textview like this, with a fixed width:
> UITextView * messageTextView = [[UITextView alloc]
> initWithFrame:CGRectMake(46.0f, 0.0f, 240.0f, messageSize.height +
> 25)];

Strike-through font in objective C

How can I use strike-through font in objective C??? More specifically in UITableViewCell
cell.textLabel.text = name;
cell.detailTextLabel.text = quantity ;
cell.XXX = ??
this is Marin, the author of the attributed strings chapter in "iOS6 by Tutorials".
Since iOS6 there is actually a native support for a bunch of different text attributes, including strike-trough.
Here's a short example, which you can use for your cell text label:
NSDictionary* attributes = #{
NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
};
NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:#"My Text" attributes:attributes];
cell.textLabel.attributedText = attrText;
That's all. Good luck!
EDIT: This answer is out of date as of iOS 6. Please see the more recent answers below
There is not native support for strike-through or underline fonts. You have to draw the lines yourself over the views for the labels.
This is silly since the font inspector for IB has options to set strike-through and underline, but these are promptly ignored if you try to set them.
CGRect frame = sender.titleLabel.frame;
UILabel *strikethrough = [[UILabel alloc] initWithFrame:frame];
strikethrough.opaque = YES;
strikethrough.backgroundColor = [UIColor clearColor];
strikethrough.text = #"------------------------------------------------";
strikethrough.lineBreakMode = UILineBreakModeClip;
[sender addSubview:strikethrough];
here is how you strikethrough your label. But remember, it only works after ios 6.0
NSNumber *strikeSize = [NSNumber numberWithInt:2];
NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize
forKey:NSStrikethroughStyleAttributeName];
NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:#"Striking through it" attributes:strikeThroughAttribute];
strikeThroughLabel.attributedText = strikeThroughText;
1-Get the size of text which needs to strikethrough
CGSize expectedLabelSize = [string sizeWithFont:cell.titleLabel.font constrainedToSize:cell.titleLabel.frame.size lineBreakMode:UILineBreakModeClip];
2-Create an line and add it to the text
UIView *viewUnderline = [[UIView alloc] init];
viewUnderline.frame = CGRectMake(20, 12, expectedLabelSize.width, 1);
viewUnderline.backgroundColor = [UIColor grayColor];
[cell addSubview:viewUnderline];
[viewUnderline release];
Strikethrough is possible by using NSStrikeThroughAttributes and attributedText of UILabel. Here is the solution in Swift
let strikeThroughAttributes = [NSStrikethroughStyleAttributeName : 1]
let strikeThroughString = NSAttributedString(string: "Text of Label", attributes: strikeThroughAttributes)
strikeThroughLabel.attributedText = strikeThroughString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:#2
range:NSMakeRange(0, [attributeString length])];
yourLabel.attributedText = attributeString;
Have you thought about finding your own strikethrough font and loading it yourself? It isn't that hard, just add the UIFont to your Info.plist file and put the name of the font in there. Then you can manually set the text to that new strikethrough font.
See this post on loading custom font.
Can I embed a custom font...
UIView *strikeView = [[UIView alloc] initWithFrame:ccr(0, 0, myLabel.bounds.size.width, 1)];
strikeView.backgroundColor = [UIColor redColor];
strikeView.center = ccp(myLabel.bounds.size.width/2, myLabel.bounds.size.height/2);
[myLabel addSubview:strikeView];
This will strike through the whole cell. In case you need it to look like completed task:
CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height / 2,size.width - 30, 1)];
line.backgroundColor = [UIColor grayColor]; // set your preferred color
[cell addSubview:line];
You may indicate some other value in CGRectMake instead of 15 - it's offset by X. In this case you'd better then decrease width by doubled value (in my case it's 15*2 = 30) in order it look nice.