UILabel multiple line in UITableViewCell - iphone

I have created a custom view cell with a UILabel in it, I have set :
cell.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.titleLabel.numberOfLines = 0;
and it's not going in multiple lines, why is this?
in the layoutSubviews I have:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect frame = titleLabel.frame;
frame.origin.y = 5;
titleLabel.frame = frame;
}
and that's about all the settings I have, however for a long text put in the label in the cell it just won't word wrap:

What is the height of the label's frame? It needs to be tall enough to hold more than one line of text. Check out Adjust UILabel height depending on the text

Related

How to limit width of textLabel in UITabelViewCell

I want to restrict the width of textLabel of UITabelViewCell as it contains a image on its right side.
I don't want to use UILabel or subclass UITabelViewCell.
Try using the accessoryView property for the image on the right. This should prevent the label from getting too wide and truncating the text.
[cell setAccessoryView:<your image view>];
cell.textLabel.numberOfLines = 3; // set the numberOfLines
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
OR
CGRect aFrame = cell.textLabel.frame;
aFrame.size.width = 100; // for example
cell.textLabel.frame = aFrame;

How to set the number of lines in uitextfield dynamically?

Actually i am trying to set the text in the textfield in next line when it becomes larger in the first line ad default. Can you please help me. my uitextfield is taken through the IB. and i am trying to write it and wants to post something like comment.
[viewFeedback setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background_Feedback.png"]]];
CGSize size = [txtFeedback.text sizeWithFont:txtFeedback.font];
CGRect f = txtFeedback.frame;
f.size.height = ceil(size.width/f.size.width)*size.height;
txtFeedback.frame = f;
txtFeedback is the textfield name here..
Well this would be a Increasing the height of the UITextfield dynamically. Please follow this or add your custom frame
In your UITextViewDelegate implementation of textViewDidChange: you need to calculate the size of the text for the specific width and adjust accordingly.
-(void)textViewDidChange:(UITextView *)textView
{
NSString *text = [textView text];
NSFont *font = [textView font];
CGFloat width = [textView frame].size.width;
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:UILineBreakModeWordWrap];
/* Adjust text view width with "size.height" */
}

UITextView will not resize

I have tried resizing a UITextView (inside a tableViewCell) to the content size. but it will not change its height at all. I have even changed the height of the UITableViewCell. What could be wrong?
- (void) setTextViewContents: (NSString*) string
{
[textView setText:string];
CGRect frame2 = self.frame;
frame2.size.height = 10000;
self.frame = frame2;
/* resize the view */
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height+60;
textView.frame = frame;
The string does appear on the view but the size does not change.
Try calling the -[UIView sizeToFit] method.

can't add subview to UIScrollView

I am trying to create a horizontal scroll view which has a UILabel as elements, by doing the following in viewDidLoad:
for(int index=0; index < [self.category count]; index++)
{
UILabel *label = [[UILabel alloc] init];
label.text = [self.category objectAtIndex:index];
label.textColor = [UIColor blackColor];
CGSize maximumLabelSize = CGSizeMake(100,9999);
CGSize expectedLabelSize = [[self.category objectAtIndex:index] sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(5+xOffset, 0, expectedLabelSize.width, 40);
self.scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[self.scrollView addSubview:label];
xOffset += 170;
}
However, I can't see anything when I run the app in the simulator, what am I missing here? Pretty sure that the UIScrollView is connected via the IBOutlet and I know that the text exists as I tried printing that out via NSLog
UPDATE:
Also how do I check which UILabel is clicked? I wanted to know this as well..
I think you did'nt initialize xOffset=0 before running for loop. so that it is taking a garbage value and then executing xOffset += 170; instruction.
so please initialize xOffset=0;
If you're positioning the labels with a frame you should also set autoresizingMask to make sure if you position based on a portrait layout but are testing the app in landscape, your labels don't get auto-positioned outside the new bounds. Also, make sure to autorelease those labels or else you'll be leaking memory.
Your expectedLabelSize calculation seems to adjust the height, leaving the width at 100. When you set the frame of your UILabel you are just using this width.

How can I change a cell's textLabel frame?

I've tried nearly everything, but I just can't seem to move the cell.textLabel property down a little bit. I've attached a screenshot below and I've tried nearly everything I could find.
I've tried changing the .frame property directly, attempted at modifying if via using the "- (void)tableView:(UITableView *)tableView willDisplayCell" method". I've also tried allocating a custom label. I could move the custom label, but it wouldn't go into separate lines like the original textLabel. I just need to move the pictured multi line label a bit down.
Any help is appreciated!
override layoutSubviews for your UITableViewCell...
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.bounds.size;
CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height);
self.textLabel.frame = frame;
self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}
or you can simply make your own UILabel object, and add it to cell.contentView as a subview.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 30, 30)];
[cell.contentView addSubview:label];
[label release];
The only way to do this is to use a UITableViewCell subclass and override -layoutSubviews. In this method, you'll want to call [super layoutSubviews], and then do any frame tweaks that you want to the label.
I ended up adding a \n in the beginning of the string. Unfortunately I couldn't get anything else to work.
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.bounds.size;
CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height);
self.textLabel.frame = frame;
self.textLabel.textAlignment = NSTextAlignmentCenter;
}