I am looking to autosize the text and display it all on one line. My problem is I don't want the "..." appended at the end of my text. How can I get rid of this? My first image is where it shows the "..." that I want to get rid of. The second image shows what happens when I change the numberOfLines on the cityLabel from 0 to 1. I also don't want this because I don't want multiple lines (just all on one line).
Here is the code:
UIView *view = [[UIView alloc] initWithFrame: CGRectMake (20, 0, self.view.frame.size.width-20, 90)];
UILabel *cityLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 5, self.view.frame.size.width-20, 55)];
UILabel *supportedCitiesLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 65, self.view.frame.size.width-20, 20)];
cityLabel.font = [UIFont boldSystemFontOfSize:50.0];
cityLabel.text = @"Dallas Dallas Dallas Dallas Dallas Dallas";
supportedCitiesLabel.text = @"Valley Test";
CGRect labelRect = cityLabel.frame;
cityLabel.adjustsFontSizeToFitWidth = NO;
cityLabel.numberOfLines = 1;
CGFloat fontSize = 50;
while(fontSize > 0.0)
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:fontSize], NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
CGSize size = [cityLabel.text boundingRectWithSize:CGSizeMake(labelRect.size.width, 10000)
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:attrDict context:nil].size;
if(size.height <= labelRect.size.height)
break;
fontSize -= 1.0;
}
cityLabel.font = [UIFont boldSystemFontOfSize:fontSize];
Set [UILabel lineBreakMode]
.
Constants
NSLineBreakByWordWrapping
Wrapping occurs at word boundaries, unless the word itself doesn't fit on a single line. See Characters and Grapheme Clusters in String Programming Guide for a discussion of issues related to determining word boundaries.
Available in iOS 6.0 and later.
NSLineBreakByCharWrapping
Wrapping occurs before the first character that doesn't fit.
Available in iOS 6.0 and later.
NSLineBreakByClipping
Lines are simply not drawn past the edge of the text container.
Available in iOS 6.0 and later.
NSLineBreakByTruncatingHead
The line is displayed so that the end fits in the container and the missing text at the beginning of the line is indicated by an ellipsis glyph. Although this mode works for multiline text, it is more often used for single line text.
Available in iOS 6.0 and later.
NSLineBreakByTruncatingTail
The line is displayed so that the beginning fits in the container and the missing text at the end of the line is indicated by an ellipsis glyph. Although this mode works for multiline text, it is more often used for single line text.
Available in iOS 6.0 and later.
NSLineBreakByTruncatingMiddle
The line is displayed so that the beginning and end fit in the container and the missing text in the middle is indicated by an ellipsis glyph. Although this mode works for multiline text, it is more often used for single line text.
Available in iOS 6.0 and later.
if you want text to autosize to fit width, why did you set cityLabel.adjustsFontSizeToFitWidth = NO;
I think it should be set to YES.
Then set cityLabel.lineBreakMode = NSLineBreakByTruncatingTail
Try it with the flowing.
UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 150, self.view.frame.size.width-20, 90)];
UILabel *cityLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 5, self.view.frame.size.width-20, 55)];
UILabel *supportedCitiesLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 65, self.view.frame.size.width-20, 20)];
cityLabel.font = [UIFont boldSystemFontOfSize:50.0];
cityLabel.text = @"Dallas Dallas Dallas Dallas Dallas Dallas";
supportedCitiesLabel.text = @"Valley Test";
CGRect labelRect = cityLabel.frame;
cityLabel.adjustsFontSizeToFitWidth = NO;
cityLabel.lineBreakMode = NSLineBreakByClipping;
cityLabel.numberOfLines = 1;
CGFloat fontSize = 50;
while(fontSize > 0.0)
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:fontSize], NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
CGSize size = [cityLabel.text boundingRectWithSize:CGSizeMake(labelRect.size.width, 10000)
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:attrDict context:nil].size;
if(size.height <= labelRect.size.height)
break;
fontSize -= 1.0;
}
cityLabel.font = [UIFont boldSystemFontOfSize:fontSize];
// [self.view addSubview:view];
// [view addSubview:cityLabel];
// [view addSubview:supportedCitiesLabel];
Some interesting options here but this can all be done inside of Interface Builder with one setting:
Also, if you have the correct AutoLayout constraints this should work dynamically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.