Getting CGSize for NSString? - iphone

I am trying to get a CGSize for an NSString but I am having issues. Here is the code I am using:
CGFloat actualMessageFontSize;
CGSize messageSize =
[message sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold"
size:14.0f]
minFontSize:14.0f
actualFontSize:&actualMessageFontSize
forWidth:(alertViewWidth - textIndent)
lineBreakMode:NSLineBreakByWordWrapping];
I NSLog the CGSize after this code and the height doesn't change, even if I put a massive NSString into it. Do you know why this might happen and or what I should try instead?
Thanks.

use this method and
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like bellow... i use this for UILable see the code..
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 31)];
lblAddress.text = #"ABDKFNKG KKGK jfnjgdf gdklfg fldgmfkgml f";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;///write this line for multiple line and set 0 or anything lese
lblAddress.font = [UIFont fontWithName:#"Helvetica" size:12];
lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] );
lblAddress.textColor = [UIColor darkGrayColor];
[self.view addSubview:lblAddress];

You have change frame of UILabel or UITextView which of them might be using to change accordingly.
CGFloat actualMessageFontSize = 12.0;
CGSize messageSize = [message sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:14.0f] minFontSize
//set frame accordingly
yourLabel.frame = CGReactMake(yourLabel.frame.origin.x,yourLabel.frame.origin.y,messageSize.width,messageSize.wiheight,);

Related

UILabel have to fit size with out break in middle of the word

I created a UILabel dynamically and i will update the data in to that dynamically.
Am getting output with many number of lines and it is fine
my problem is word break in middle to the next line although i use line break mode word wrap
Help me out of this am googling it from 5hours.
The data passed to that UILabel is orthogonist,endonist,psychologist,cardioligist
CGSize constraint8 = CGSizeMake(190, 2000.0f);
CGSize size8=[temp sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:14] constrainedToSize:constraint8 lineBreakMode:UILineBreakModeWordWrap];
specialities1 =[[UILabel alloc]init];
[specialities1 setFrame:CGRectMake(124,218,190, size8.height)];
specialities1.textAlignment=UITextAlignmentLeft;
specialities1.backgroundColor=[UIColor clearColor];
specialities1.numberOfLines=0;
specialities1.lineBreakMode=UILineBreakModeClip;
specialities1.font=[UIFont fontWithName:#"Helvetica-Bold" size:14];
specialities1.text=[NSString stringWithFormat:#"%# ",temp ];
[specialities1 sizeToFit];
[testscroll addSubview:specialities1];
Change:
specialities1.lineBreakMode=UILineBreakModeClip;
to :
specialities1.lineBreakMode= UILineBreakModeWordWrap;
Change both this line in your code.
According to your code above you are setting 0 lines in your label. So change it to,
specialities1.numberOfLines=size8.height/12;//(=1000, or whatever according to your requirement)
specialities1.lineBreakMode= UILineBreakModeWordWrap;
Source
Get size
NSString *text = [dicOtherInfo objectForKey:#"msg"];
CGSize constraint = CGSizeMake(200, 1000.0f);
CGSize size = [text sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
Set Size
UILabel *lblMsg=[[UILabel alloc] initWithFrame:CGRectMake(5,15, size.width, size.height)];
[lblMsg setLineBreakMode:UILineBreakModeWordWrap];
lblMsg.text = text;
lblMsg.numberOfLines = (size.height/16);
lblMsg.textAlignment=UITextAlignmentLeft;
lblMsg.backgroundColor=[UIColor clearColor];
lblMsg.textColor=[UIColor redColor];
[lblMsg setFont:[UIFont fontWithName:#"Verdana" size:12]];
Try my below method:
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like below:
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 50)];
lblAddress.text = #"your Text ";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;
lblAddress.font = [UIFont fontWithName:#"Helvetica" size:12];
lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] );
lblAddress.textColor = [UIColor darkGrayColor];
[self.view addSubview:lblAddress];

UILabel's numberofLines issue

I have to display some text(dynamic) which is in three string variables like this:
SanFransisco,California 32122 i.e city,state zipCode
So thought of displaying them in three labels.But I couldnot understand when to use sizetoFit and when not to.
If it is a large text like BrightWood Park,District of Columbia 32123 then Im getting it like BrightWood Park,District of Columb .I couldnot see the zipCode part on the simulator.So Whatever the text mightbe it should be displayed in the simulator.
If city's text is large like Massachusetts Avenue Heights,District of Columbia 32123 then it should be displayed like Massachusetts Avenue Heights,District of
Columbia 32123
Currently Im getting as Massachusetts Avenue Heights,District of Co
NSString *city=[NSString stringWithFormat:#"%#,",self.city];
CGSize constraint4 = CGSizeMake(250, 2000.0f);
CGSize size4=[city sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
lblCity=[[UILabel alloc] init];
[lblCity setFrame:CGRectMake(60,Lane1.frame.size.height+Lane1.frame.origin.y,size4.width,size4.height) ];
lblCity.textAlignment=UITextAlignmentLeft;
lblCity.backgroundColor=[UIColor clearColor];
lblCity.text=[NSString stringWithFormat:#"%#",city];
[lblCity setNumberOfLines:0];
lblCity.highlightedTextColor=[UIColor greenColor];
[lblCity setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[testscroll addSubview: lblCity];
NSString *state=[NSString stringWithFormat:#"%# ",self.state];
CGSize constraint5 = CGSizeMake(250, 2000.0f);
CGSize size5=[state sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
lblState=[[UILabel alloc] init];
[lblState setFrame:CGRectMake(lblCity.frame.origin.x+lblCity.frame.size.width,Lane1.frame.size.height+Lane1.frame.origin.y,size5.width,size5.height) ];
lblState.textAlignment=UITextAlignmentLeft;
lblState.backgroundColor=[UIColor clearColor];
lblState.text=[NSString stringWithFormat:#"%#" ,state];
[lblState setNumberOfLines:0];
lblState.highlightedTextColor=[UIColor greenColor];
[lblState setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[testscroll addSubview: lblState];
NSString *zip=[NSString stringWithFormat:#"%#",self.zip];
CGSize constraint200=CGSizeMake(250,2000.0f);
CGSize size200=[zip sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]constrainedToSize:constraint200 lineBreakMode:UILineBreakModeWordWrap ];
zipCode=[[UILabel alloc] init];
[zipCode setFrame:CGRectMake(lblState.frame.origin.x+lblState.frame.size.width,Lane1.frame.size.height+Lane1.frame.origin.y,size200.width,size200.height) ];
zipCode.textAlignment=UITextAlignmentLeft;
zipCode.backgroundColor=[UIColor clearColor];
zipCode.text=[NSString stringWithFormat:#"%#" ,zip];
[zipCode setNumberOfLines:0];
zipCode.highlightedTextColor=[UIColor greenColor];
[zipCode setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[testscroll addSubview:zipCode];
2.When to use sizetoFit along numberofLines=0 and when not to ?
There are several important points to make autofit UILabel.
At first you have to set label's height with zero
At second you have to set label' numberOfLines to zero too
And in the end you have to call sizeToFit method
Like this:
label.frame = CGRectMake(x, y, width, 0.0f);
label.numberOfLines = 0;
[label sizeToFit];
After get the string, set UILabel size like this
lblCity.text=[NSString stringWithFormat:#"%#",city];
[lblCity setNumberOfLines:0];
[lblCity setFrame:CGRectMake(60,Lane1.frame.size.height+Lane1.frame.origin.y,size4.width,size4.height) ];
[lblCity sizeToFit]; it may be work
///here i tested work fine and show whole text
NSString *city=#"Orlando ";
CGSize constraint4 = CGSizeMake(250, 2000.0f);
CGSize size4=[city sizeWithFont:[UIFont fontWithName:#"Helvetica-Bold" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"size4.width %f height %f ",size4.width,size4.height);
UILabel* lblCity=[[UILabel alloc] init];
[lblCity setFrame:CGRectMake(160,16,size4.width,size4.height) ];
lblCity.textAlignment=UITextAlignmentLeft;
lblCity.backgroundColor=[UIColor clearColor];
lblCity.text=[NSString stringWithFormat:#"%#",city];
[lblCity setNumberOfLines:0];
lblCity.highlightedTextColor=[UIColor greenColor];
[lblCity setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[lblCity sizeToFit];
[self.view addSubview: lblCity];

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

Displaying parse string as label issue

I've got a label which is displayed in a subview. I got the text of the label from a NSString on parse. However, I tried to display the text but it doesnt seems to work. There is no text displayed.
Here is my code. Please let me know if you know how to solve this:
PFQuery *query = [PFQuery queryWithClassName:#"myapp"];
[query getObjectInBackgroundWithId:#"myId"
block:^(PFObject *textu, NSError *error) {
if (!error) {
CGRect infoLabelRect = CGRectMake(10, 250, 260, 350);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
NSString *text = [textu objectForKey:#"newstext"];
UIFont *font = nil;
CGFloat points = 17;
CGFloat maxHeight = infoLabel.frame.size.height;
CGFloat textHeight;
do {
font = [UIFont systemFontOfSize:points];
CGSize size = CGSizeMake(infoLabelRect.size.width, 100000);
CGSize textSize = [text sizeWithFont:font constrainedToSize:size lineBreakMode: NSLineBreakByWordWrapping];
textHeight = textSize.height;
points -= 1;
} while (textHeight > maxHeight);
infoLabel.font = font;
infoLabel.numberOfLines = 9;
infoLabel.textColor = [UIColor whiteColor];
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.shadowColor = [UIColor blackColor];
infoLabel.shadowOffset = CGSizeMake(0, 1);
[infoLabel sizeToFit];
[contentView addSubview:infoLabel];
} else {
// Log details of our failure
CGRect infoLabelRect = CGRectMake(10, 250, 260, 350);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = #"Connection error!";
}
}];
Your first part of code doesn't contain assignment to text
infoLabel.text = text; // or #"Some text" for testing
Your last part of code only creates UILabel, but doesn't add it as subview
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = #"Connection error!";
[contentView addSubview:infoLabel];

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