UILabel text gets cut off - objective-c

I'm trying to dynamicly set label size. It works in a strange way, i get some of the text cut off.
I first set my label text and then try to resize it like this way.
_switch2Label.text = #"Call on alarm, there will be no call if other user of alarm system will recieve an alarm call and confirm (answer) it by pressing 0#";
_switch2Label.numberOfLines = 0;
[self newFrame:_switch2Label];
- (void) newFrame:(UILabel *) label
{
CGSize maxSize = self.view.bounds.size;
maxSize.width = maxSize.width - 30;
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];
CGRect newFrame = label.frame;
newFrame.size.height = labelSize.height;
label.frame = newFrame;
}
I only get three lines of text, while five is needed for this label. Maybe anyone could see my mistake here? If I add more text to label it gets shown, yet still about two lines of the label text gets cutt off.

I have changed your method...Please check it..it may help you..
- (void) newFrame:(UILabel *) label
{
CGSize constraint = CGSizeMake(300, 1000.0f);
CGSize size_txt_overview1 = [label.text sizeWithFont:[UIFont fontWithName:#"Arial Rounded MT Bold" size:15] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(20,20, size_txt_overview1.width, size_txt_overview1.height+15);
}

_switch2Label.text = #"Call on alarm, there will be no call if other user of alarm system will recieve an alarm call and confirm (answer) it by pressing 0#,";
_switch2Label.numberOfLines = 0;
[self newFrame:_switch2Label];
- (void) newFrame:(UILabel *) label
{
CGSize maximumSize = CGSizeMake(label.frame.size.width, 10000);
//maxSize.width = maxSize.width - 30;
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = label.frame;
newFrame.size.height = labelSize.height;
label.frame = newFrame;
}
Use this code blocks , may help u.

Why programatically resizing the label? Is this something you cannot do in IB or using autorezizeMask?

The label's constraint size isn't getting calculated as you intend, currently, your code is constraining the label height to the view's bound's height. Changing your maxSize instance to:
CGSize maxSize = CGSizeMake(self.view.bounds.size.width - 30, MAXFLOAT);
CGSize labelSize = ...
Doing so will ensure that the constraint is not bound by your view bounds. You may also want to consider setting the clipsToBounds property of your view if you want the label to be able to extend past your view's bounds.

Related

Objective c uilabel width based on text length

I want to set the width of the uilabel based on the text length and then on top of that make it a little bit wider. The text might be different so I cannot just predefine the width of the uilabel. My approach to this is to get the width of the text and then add another 10.0 to it but it doesn't work.
CGSize tSize = [[self.label1 text] sizeWithAttributes:#{NSFontAttributeName:[self.label1 font]}];
CGFloat tWidth = tSize.width + 10.0;
self.label1.frame = CGRectMake(self.label1.frame.origin.x, self.label1.frame.origin.y, tWidth, self.label1.frame.size.height);
self.label1.backgroundColor = [UIColor blueColor];
Any advice is much appreciated.
You might want to use [yourLabel sizeToFit]; or [yourLabel sizeThatFits: <#your target size#>];
sizeToFit resizes your label within it's current frame.size according to it's current text , font and the numberOfLines you provided.
Then you can change the size
[yourLabel sizeToFit];
CGRect rect = yourLabel.frame;
rect.size.width += 10;
yourLabel.frame = rect;
sizeThatFits:(CGSize)size doesn't resize your label but it returns the size that fits the size you pass as parameter.
sizeToFit uses sizeThatFits:(CGSize)size internally.
You can change size by doing this
CGSize exampleSize = (CGSize){300, 300};
CGSize fittingSize = [yourLabel sizeThatFits: exampleSize];
CGRect rect = yourLabel.frame;
rect.size.width = fittingSize.width;
rect.size.height = fittingSize.height;
yourLabel.frame = rect;
This should be working fine even if you are working with yourLabel.attributedText

sizeWithFont: and sizeWithAttributes: don't work for a long single line

I'm trying to set a dynamic height for cells in my table, height should be based on a text length and a max width.
The problem appears when this text comes in a single line, without line separators. Doesn't matter how large the text is, if there are no line separators it detects that text fits in a single line so my cell height doesn't increase.
Am I doing something wrong? How can I achieve it? Thanks.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat cellheight = 35.0f; // BASE
NSString *text = #"...";
if (text) {
UIFont *font = (indexPath.row == 0) ? [UIFont systemFontOfSize:14] : [UIFont systemFontOfSize:12];
CGSize constraintSize = CGSizeMake(self.tableView.frame.size.width, CGFLOAT_MAX);
if (IS_EARLIER_THAN_IOS7) {
CGSize size = [text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByCharWrapping];
cellheight += size.height;
} else {
CGSize size = [text sizeWithAttributes:#{NSFontAttributeName: [UIFont systemFontOfSize:12.0f]}];
CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
cellheight += adjustedSize.height;
}
return (indexPath.row == 0) ? cellheight + 40.0f : cellheight;
}
}
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:") __TVOS_PROHIBITED; // NSTextAlignment is not needed to determine size
You should use "boundingRectWithSize:options:attributes:context:" not "sizeWithAttributes:".
This is a sample
CGSize size = [text boundingRectWithSize:CGSizeMake(_myTableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
There is a way more easy way to do that:
First set the text to the UILabel, and set all the required font, size. etc. Then call sizeThatFits method on the label.
CGSize sizze =[itemLabel sizeThatFits:CGSizeMake(itemNameLabelWidth, CGFLOAT_MAX)];
Also dont forget to set numberOfLines and lineBreakMode before calling sizeThatFits:
itemLabel.numberOfLines=0;
itemLabel.lineBreakMode=NSLineBreakByWordWrapping;
Note 1 : calling sizeThatFits does not set the new frame to the UILabel, it just calculates and returns the new frame. You have to then set the frame to the label by adding x and y origin values. So that becomes :
CGSize sizze =[itemLabel sizeThatFits:CGSizeMake(itemNameLabelWidth, CGFLOAT_MAX)];
CGRect namelabelFrame = itemLabel.frame;
namelabelFrame.size = sizze;
itemLabel.frame = namelabelFrame;
Note 2 : This code is okay in cellForRowAtIndexPath:, but when calculating the height inside heightForRowAtIndexPath: you may want to optimize this code a little bit. As you don't have the cell to work with you might initialize a UILabel object and perform this code on it to estimate the height. But having UIView initializations inside heightForRowAtIndexPath: is not a good idea as they can significantly affect performance while scrolling.
So what you do is have an already initialised (and all formatting applied) UILabel as a class variable and reuse that for height calculation.

Resizing UITextView automatically

I'm a beginner in iOS development, and I have trouble resizing automatically a UITextView.
I created a Master-Detail Application. On my DetailViewController.xib I added a ScrollView that encompass a Label for the title, an image and under this image, a TextView in order to put a description that the user posted.
The problem is that the description depends on what my WebService returns, so I need it to be resized automatically.
Here's my code:
- (void)configureView
{
...
self.detailDescriptionTextView.text = [self.detailItem valueForKey:#"adDescription"];
CGRect frame = self.detailDescriptionTextView.frame;
frame.size.height = self.detailDescriptionTextView.contentSize.height;
self.detailDescriptionTextView.frame = frame;
)
The problem is that the TextView gets the content correctly but it doesn't resize at all.. I looked for an answer for a while and most of answers are what I tried...
Thanks for your help.
EDIT : I realized something. In order to put some elements under the description, I've put a very small TextView for the description in the Interface Builder, is it a problem? If it is, how could I put other elements under this TextView, because there would be no more space on the Interface Builder no?
EDIT 2 : I finally succeeded. It seems like the problem was that I created the TextView by Interface Builder. By creating it programmatically, it worked perfectly. Here's my code if it can help someone:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 195, 280, 10)];
textView.text = [self.detailItem valueForKey:#"adDescription"];
textView.font = [UIFont fontWithName:#"Hevlatica" size:14];
[self.scrollView addSubview:textView];
CGRect descriptionFrame = textView.frame;
descriptionFrame.size.height = textView.contentSize.height;
textView.frame = descriptionFrame;
Thanks for your help.
First calculate the content height using
NSString *content = #"Hello how are you.";
CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(yourWidth, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
and then set the frame of textView
[self.textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];
Or
Add the content to your textview and then try this
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Hope this Helps !!!
CGSize constraint = CGSizeMake(690.0, 2000.0);
int h=10;
CGSize size_txt_overview1 = [[self.detailItem valueForKey:#"adDescription"] sizeWithFont:[UIFont fontWithName:#"Helvetica" size:18] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
frame.size.height = size_txt_overview1.height;
self.detailDescriptionTextView.frame = frame;

How to change the width of label once after its frame has been set? and to get the width of NSString

hope you are doing good.
I am setting a label and adjusting its frame ..
UILabel *mylabel=[[UILabel alloc]init];
mylabel.Text=#"This is my label";
mylabel.frame=CGRectMake(10,10,200,21);
now if I change the Label's text from any string set by me like this
NSString *mystring=#"This is my first string that is to be assigned to my label dynamilcally";
mylabel.Text=mystring;
mylabel.frame.size.width=mystring.length;
but nothing happens. The width of the label remains 200, as I set while at the time of initialisation.
This will also need to get the width of the mystring . How to get that?
Thank in anticipation for helping me a lot.
[myLabel sizeToFit] if you need it on one line. For multiple line labels you can use NSString's sizeWithFont:constrainedToSize:lineBreakMode: function to get size of your string and use it to modify your label's height & width.
e.g. code that gives you height for multiline label with fixed width of 200px:
CGSize textSize = {
200.0, // limit width
20000.0 // and height of text area
};
CGSize size = [descriptionString sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height < 36? 36: size.height; // lower bound for height
CGRect labelFrame = [myLabel frame];
labelFrame.size.height = height;
[myLabel setFrame:labelFrame];
[myLabel setText:descriptionString];

heightForRowAtIndexPath not displaying multiline

heightForRowAtIndexPath is never displaying multiline when the text is longer than usual.
Here is my code:
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat result = 44.0f;
CGFloat width = 0;
CGFloat tableViewWidth;
CGRect bounds = [UIScreen mainScreen].bounds;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
tableViewWidth = bounds.size.width;
else
tableViewWidth = bounds.size.height;
width = tableViewWidth - 110; // fudge factor, 115 isn't quite right
NSUInteger index = [indexPath row];
id doc = [[self displayedObjects] objectAtIndex:index];
NSString *title = [doc title];
if (title)
{
// The notes can be of any height
// This needs to work for both portrait and landscape orientations.
// Calls to the table view to get the current cell and the rect for the
// current row are recursive and call back this method.
CGSize textSize = { width, 20000.0f }; // width and height of text area
CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 29.0f; // top and bottom margin
result = MAX(size.height, 44.0f); // at least one row
}
return result;
}
Any help will be really appreciated.
Thanks!
To make label show multiline text you must set its numberOfLines property to a maximum allowed number of lines (or to 0 for arbitrary lines number). So in cellForRowAtIndexPath: method when setup your cell add:
...
cell.textLabel.numberOfLines = 0;
...
I suppose you don't use a custom UITableViewCell. So you're probably setting the title somewhere with something similar to the following code :
cell.textLabel.text = #"long long long text";
To get your text on multiple lines you have to configure the default title UILabel to display its text on multiple lines. Like this :
cell.textLabel.numberOfLines = 3;
You can compute the number of line the same way you compute the height of the cell. In fact I think you have to compute both to do what you want (height adapted to the content).
CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
What matters in textSize is to set the row width for the width attribute and an height value which is "large" enough to "host" the text.
Once you have your size initialized you can divide the height by the height of the UILabel line.