UILabel *label = [[UILabel alloc] init];
label.text = #"Hello";
[label sizeToFit];
CGSize size = label.frame.size;
Usually we get the size of the label in this way, but text in the UILabel does not fill all space in it, there's always some margin around the text. That makes drawing label difficult to be exactly the same as visual draft. Anyone could help ?
for example: If you want to put an icon at the bottom of some text, let the margin between them be, for example 50px, and you write icon.frame = CGRectMake(10,textLabel.frame.size.height + textLabel.frame.origin.y + 50, 100, 100); but since the text can't fill all space in UILabel, so the margin in this way should be a bit larger than it should be. So I want to figure out a better way, thanks.
try with this bellow method which returns dynamic height of UILable with its text content... we this method you can set frame of UILable with its text content...
-(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...
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];
See My Blog..
i hope you get some idea from this post...
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).
I have a problem regarding to set UIButton frame.
NSString *sample = #"Demo to set display view on label in iPhone";
CGSize maximumLabelSize = CGSizeMake(55,9999);
expectedLabelSize = [sample sizeWithFont:[UIFont systemFontOfSize:20]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"%f",expectedLabelSize.height);
self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, expectedLabelSize.width, expectedLabelSize.height)];
self.label.backgroundColor = [UIColor clearColor];
self.label.numberOfLines = 0;
self.label.text = sample;
[self.label sizeToFit];
So in below uibutton code how to set frame position of text so that only "iPhone" word is clickable?
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(self.label.frame.origin.x,
self.label.frame.origin.y+50,
expectedLabelSize.width,
expectedLabelSize.height);
To make just one word of a UILabel clickable as a hyperlink you would need to know exactly where the UILabel puts that word. That is difficult (not impossible) especially with a multiple line label and especially if you allow resizing of the font to make the text fit.
Why not use a UIWebView and make it a hyperlink instead? Or separate the text that is clickable and put that text into the UIButton by itself, like:
label.text = #"Demo to set display view on label in ";
button.titleLabel.text = #"iPhone";
Note: you should not checkmark this answer if your problem is not solved.
I have a UILabel coded programmatically. I want to change the size of the label when i pressed a button. how to change the size of that label? this is my code
UILabel *theLabel11 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)];
[theLabel11 setText:#"US"];
[theLabel11 setTextAlignment:UITextAlignmentCenter];
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
[theLabel11 setBackgroundColor:[UIColor orangeColor]];
[theLabel11 setTextColor:[UIColor blackColor]];
[scroll1 addSubview:theLabel11];
You should declare your label as class property, so it can be accessed from other methods
To change the font size use
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
To change the frame size of the label us
theLabel11.frame = CGRectMake(x, y, width, height);
A common idiom for adjusting the spatial information on a UIView is as below
label.frame = CGRectMake(
x,
y,
width,
height
);
You can get the old position and height via
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height
If there is only one label added to scroll1 then, iterate the scrollview to get the label reference as follows in button action
for(UIView *subView in scroll1.subViews){
if([subView isKindOfClass:[UILabel class]]){
UILabel *lbl=(UILabel*)subView;
//change size of label here
}
}
if there are many labels assign a tag to each label while creating and check that in for loop
My UITextView has only 1 line of text. But the bottom half of the text is being cut off, the text seems to be drawn to low, or the Text View seems to have some height that it needs to be which is larger than the 15 i am setting it too.
UIFont *font = [UIFont fontWithName:#"Marker Felt" size:13];
CGSize size = CGSizeMake(310, 1000.f);
CGSize stringSize = [[messagesArray objectAtIndex:indexPath.row]
sizeWithFont:font constrainedToSize:size];
NSLog(#"txtView height: %d",(int)stringSize.height);
UITextView *txtview = [[UITextView alloc] initWithFrame:CGRectMake(5, 0,
310, (int)stringSize.height)];
txtview.autoresizesSubviews = NO;
[txtview setBackgroundColor:[UIColor blueColor]];
[txtview setFont:[UIFont fontWithName:#"Marker Felt" size:13]];
txtview.textColor = [UIColor whiteColor];
[txtview setOpaque:NO];
txtview.editable = NO;
txtview.text = [messagesArray objectAtIndex:indexPath.row];
[cell addSubview:txtview];
This line
NSLog(#"txtView height: %d",(int)stringSize.height);
Always prints out the value of 15. The Font is off size 13, so I would have expected the text to be positioned vertically centered in the UITextView with 1 pixel free at the top and the bottom. 15-13 etc.
Is there something that I am completely missing regards to setting up UITextFields?
Many Thanks,
Code
There is a default padding on UITextView. Try this:
[txtView setContentInset:UIEdgeInsetsMake(-8,-8,0,0)];
Maybe you need to play with the values :)
The problem is that by default UITextView adds about 32 to the bottom as the contentInset. You can get around this easily by subclassing the UITextView and overriding the contentInset method to return what you want like so:
#interface EditItemText : UITextView {
}
#end
#implementation EditItemText
// override the contentInset method so the scrolling behaviour of the text view is normal
- (UIEdgeInsets) contentInset {
//I am padding with 4 but you could return UIEdgeInsetsZero;
return UIEdgeInsetsMake (4,4,4,4);
}
#end
Then instead of a UITextView you would use your new subclass:
EditItem *vwTxt = [[[EditItemText alloc] initWithFrame:CGRectMake(10, 8.0, 170, 70.0)] autorelease];