Reputation: 5208
I have a UILabel
that contains some text that is more than the size of UILabel
. I don't want to change UILabel size
i only want one line in it. So my question is how can i skip(remove) the text from end that is causing the UILabel
to truncate tail?
Upvotes: 1
Views: 270
Reputation: 136
I was doing a similar thing on UITableView Cell recently. This approach is flexible, as you can set any number of lines depending on height you have available. Everything else will be truncated. Check it out:
CGSize maxNameLabelSize = CGSizeMake(350,60); //setting height, you can limit it to one line depending on the font
UILabel *eventName = (UILabel*) ([activityCell.contentView viewWithTag:EVENT_TITLE]);
//Set value here
eventName.text = event.name;
CGSize expectedNameLabelSize = [eventName.text sizeWithFont:eventName.font constrainedToSize:maxNameLabelSize lineBreakMode:eventName.lineBreakMode];
CGRect newNameFrame = eventName.frame;
newNameFrame.size.height = expectedNameLabelSize.height;
eventName.frame = newNameFrame;
Does it makes sense?
Upvotes: 0
Reputation: 25692
Try this,
I think you are asking for character wrapping trick.
First try in XIB
yourLabel.numberOfLines = 1;
yourLabel.lineBreakMode = NSLineBreakByCharWrapping;
//or
yourLabel.lineBreakMode = NSLineBreakByClipping;
Upvotes: 0
Reputation: 3359
I'm afraid there is no a direct method to calculate. But as you can get the size or bounds of a text using boundingRectWithSize:options:attributes: you can iterate using a dichotomic search algorithm and find the string position to cut from.
Upvotes: 1
Reputation: 71
Use this line to set up the line break mode of your UILabel:
self.lineBreakMode = UILineBreakModeWordWrap
Upvotes: 0