gpt4 book ai didi

ios - 调整文本大小以适合时,如何删除 UILabel 末尾的 "..."?

转载 作者:行者123 更新时间:2023-11-28 21:39:58 25 4
gpt4 key购买 nike

我希望自动调整文本大小并将其全部显示在一行中。我的问题是我不想在文本末尾附加“...”。我怎样才能摆脱这个?我的第一张图片显示了我想要摆脱的“...”。第二张图片显示了当我将 cityLabel 上的 numberOfLines 从 0 更改为 1 时发生的情况。我也不想要这个,因为我不想要多行(只在一行上)。

代码如下:

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];

图 1... Image 1

图 2... Image 2

最佳答案

设置[UILabel lineBreakMode]

Reference :

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.

关于ios - 调整文本大小以适合时,如何删除 UILabel 末尾的 "..."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32547546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com