Reputation: 612
I've read several posts on this topic: UILabel - auto-size label to fit text?, Swift - Adjusting fontSize to fit the width of the layout (programmatically), and http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift. It seems that I am following exactly what these posts suggested but it seems to not work.
The following is currently what my simulator shows:
I've set the constraints correctly, I think and I wrote the following like so:
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 150
}
And I wrote:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CathyTaskLogTableViewCell
cell.messageLabel.sizeToFit()
cell.messageLabel.text = "If you'd like to have more control over the way objects are synced, you can keep them in the local datastore until you are ready to save them yourself using saveInBackground. To manage the set of objects that need to be saved, you can again use a label. The fromPinWithName: method on PFQuery makes it easy to fetch just the objects you care about."
return cell
}
And I've set the lines lines
to 0
like so:
For the label
, I set all four constraints and the width and height constraints. And my constraints are like so:
However, it is not causing all of my lines to show. Any suggestions on how to fix this? Thank you so much in advance!!!
Upvotes: 1
Views: 3223
Reputation: 418
Use this if you want to add a custom label in the default table view cell.
the problem we want to resolve.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if var cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") {
self.customCellLabelTitle = {
let lbl = UILabel(frame: CGRect(x: 90, y: cell.textLabel?.frame.origin.y ?? 2, width: self.tblView.frame.size.width - 90, height: 40))
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.numberOfLines = 0
lbl.setContentCompressionResistancePriority(.required, for: .horizontal)
lbl.backgroundColor = .clear
lbl.text = "cell text value"
lbl.adjustsFontSizeToFitWidth = true
lbl.minimumScaleFactor = 0.01
return lbl
}()
}
Output :
Upvotes: 0
Reputation: 932
This is Objective-C solution.
At first, in your constraints
for UILabel
set height value is >= to any value (example set height constraint >= 44), see in attached image.
Add below method in your controller to get the height
of the string text,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
In above method , font
is nothing but the font what you are using for your label.
Override tableview's heightForRowAtIndexPath
, it looks like as follows,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self getHeightForText:[self.valuesArray objectAtIndex:indexPath.row] havingWidth:323.0f andFont:[UIFont fontWithName:@"Helvetica" size:18.0f]].height;
}
In cellForRowAtIndexPath
method set your text to label in normal way. This label will be automatically expand based on the size of the text;
Upvotes: 0
Reputation: 168
There is an another option that first map an outlet of your Height constraint,then in heightForRow delegate method of UITableView, calculate the height of your text then return that calculated height plus another UIElements height which are being used on cell like label for date and time as i can see in cell of simulator,then in cellForRow method again calculate height of text and assign that height to mapped object of height constraint and finally update constraints of label like [your label updateConstraints] i hope it will work.
Upvotes: 0
Reputation: 62
use this it is working for me
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
data=@[@"123454",@"122",@"thi is subbareddy",@"This is subbs reddy working as software employee trying to test dynamic heighjt for sa label"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"Cell";
CustomTableViewCell * Cell=(CustomTableViewCell*)[self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];
if (Cell==nil) {
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
Cell=[nib objectAtIndex:0];
}
UILabel *lblLoadId=(UILabel *)[Cell viewWithTag:1];
lblLoadId.text=[data objectAtIndex:indexPath.row];
lblLoadId.numberOfLines=0;
CGFloat rowHeight = [self heightForText:lblLoadId.text];
lblLoadId.frame = CGRectMake(0, 0, 300, rowHeight);
return Cell;
}
- (CGFloat)heightForText:(NSString *)bodyText
{
UIFont *cellFont = [UIFont systemFontOfSize:17];
CGSize constraintSize = CGSizeMake(300, MAXFLOAT);
CGSize labelSize = [bodyText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = labelSize.height + 50;
NSLog(@"height=%f", height);
return height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *labelText =[data objectAtIndex:indexPath.row];
return [self heightForText:labelText];
//return 100;
}
Upvotes: 0
Reputation: 2423
You should remove height of label and add new constraint from bottom of a label to tableview..It might solve your problem.Because if you add constraint height of label then it will fix your label's height and if you put bottom constraint then no matter how much your content it will put fix pixel height from bottom.
Upvotes: 1
Reputation: 62
Use custom cell in the tableview then depending on the count of letters increase height constraint it will work surely because some problem we got.
Upvotes: 0