Xcode UILabel 错误?带有 UILabel 的行间距裁剪文本
Xcode UILabel bug? Line spacing cropping text with a UILabel
我正在为一个 iOS 项目设计一些设计。使用的字体是 Avenir,行距相对紧凑。
其中一些标签将包含动态文本,因此我不能只是将标签的大小调大,因为大小应由内容决定。
默认情况下,UILabel 的行间距非常大。
如果我调整 Line Height Multiple
或 Max Height
,顶部的文本最终会被裁剪掉。
它应该像这样(Affinity Designer)...
有办法处理吗?
感谢您的帮助!
不幸的是,UILabel 在垂直调整方面有几个问题。一个有点 hacky 的解决方案是根据需要将第一行的基线向下移动。根据您的字符串是否以换行符结尾,以及您执行的收紧程度,您可能还需要添加一两个额外的换行符,否则渲染引擎将剪掉最后一行。
该代码片段假定 self.label
已经分配了一个属性字符串,并且它在两行之间有行分隔符 0x2028
。在 IB 中输入 multi-line 文本时通常如此。
// 0x2028 is the unicode line separator character
// Use \n instead if it is what you have
// or calculate the length of the first line in some other way
NSInteger lengthOfFirstLine = [self.label.text componentsSeparatedByString:@"\u2028"][0].length;
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithAttributedString:self.label.attributedText];
// Add two more blank lines so that the rendering engine doesn't clip the last line
[s appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
// Move the baseline offset for the first line down
// the other lines will adjust to this
// 50 is a value you will have to find what looks best for you
[s addAttribute:NSBaselineOffsetAttributeName value:@(-50) range:NSMakeRange(0, lengthOfFirstLine)];
self.label.attributedText = s;
这对我有用。通过添加
minimumLineHeight
let string = NSMutableAttributedString(string: venue.name)
let style = NSMutableParagraphStyle()
style.lineHeightMultiple = 0.68
style.minimumLineHeight = nameLabel.font.lineHeight
string.addAttribute(NSAttributedString.Key.paragraphStyle,
value: style,
range: NSMakeRange(0, venue.name.count))
nameLabel.attributedText = string
我正在为一个 iOS 项目设计一些设计。使用的字体是 Avenir,行距相对紧凑。
其中一些标签将包含动态文本,因此我不能只是将标签的大小调大,因为大小应由内容决定。
默认情况下,UILabel 的行间距非常大。
如果我调整 Line Height Multiple
或 Max Height
,顶部的文本最终会被裁剪掉。
它应该像这样(Affinity Designer)...
有办法处理吗?
感谢您的帮助!
不幸的是,UILabel 在垂直调整方面有几个问题。一个有点 hacky 的解决方案是根据需要将第一行的基线向下移动。根据您的字符串是否以换行符结尾,以及您执行的收紧程度,您可能还需要添加一两个额外的换行符,否则渲染引擎将剪掉最后一行。
该代码片段假定 self.label
已经分配了一个属性字符串,并且它在两行之间有行分隔符 0x2028
。在 IB 中输入 multi-line 文本时通常如此。
// 0x2028 is the unicode line separator character
// Use \n instead if it is what you have
// or calculate the length of the first line in some other way
NSInteger lengthOfFirstLine = [self.label.text componentsSeparatedByString:@"\u2028"][0].length;
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithAttributedString:self.label.attributedText];
// Add two more blank lines so that the rendering engine doesn't clip the last line
[s appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
// Move the baseline offset for the first line down
// the other lines will adjust to this
// 50 is a value you will have to find what looks best for you
[s addAttribute:NSBaselineOffsetAttributeName value:@(-50) range:NSMakeRange(0, lengthOfFirstLine)];
self.label.attributedText = s;
这对我有用。通过添加
minimumLineHeight
let string = NSMutableAttributedString(string: venue.name)
let style = NSMutableParagraphStyle()
style.lineHeightMultiple = 0.68
style.minimumLineHeight = nameLabel.font.lineHeight
string.addAttribute(NSAttributedString.Key.paragraphStyle,
value: style,
range: NSMakeRange(0, venue.name.count))
nameLabel.attributedText = string