UILabel 文本换行问题
UILabel text wrapping issue
我有一个多行 UILabel
,文本为“我想在印度洋乘船”。在 iPhone 5 中,由于宽度大小,UILabel
在第二行显示文本“ocean”。
我想要的是如果UILabel
中的文本出现在两行中,那么文本“indian ocean”应该显示在第二行或者宽度足够的情况下可用,所有文本应显示在一行中。
关于如何实现这一点的任何想法。
这是一个变通办法。但这适合您的要求,无需确定应用程序运行的设备。通常 UILabel
将单词包裹在 whitespaces
周围。因此,您可能应该将 indian ocean
设为一个单词,但它仍应显示为两个单独的单词。
let string = "<font size = 5 face = Avenir Next>I would like to ride a boat in<font color = white>_</font>indian ocean</font>"
let attributedString = try? NSAttributedString(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)
label.attributedText = attributedString
您可以指定所需的字体大小、系列并将 _
的颜色设置为 UILabel
的 backgroundColor
。
- 您应该计算 iPhone 宽度。
- 计算标签中的文本宽度。
- 如果文本宽度 > iPhone 宽度则在新行中设置 "indian ocean"
请检查以下代码:
let screenRect = UIScreen.mainScreen().bounds;
let screenWidth = screenRect.size.width;
let text:String = self.label.text!
let attr:NSDictionary = [NSFontAttributeName: self.label.font];
let textSize = (text as NSString).sizeWithAttributes(attr as? [String : AnyObject])
let textWidth = textSize.width
if textWidth > screenWidth{
self.label.text = "I would like to ride a boat in\nindian ocean"
}
label.numberOfLines = 0;
label.text = @"I would like to ride a boat in indian ocean";
[label sizeToFit];
无需使用设备检测...使用一个技巧即可解决此问题 "if label displays the text in two lines, then the text "indian ocean" 将显示第二行或显示在第一行。"
要遵循的步骤:
- 在属性检查器中将
UILabel
的文本类型从纯文本更改为属性。
- 在 "indian ocean" 之间输入任何字符,例如"indian_ocean".
- Select 那个字符(在我的例子中我使用下划线“_”)并更改文本颜色并将不透明度设置为 0%(不需要更改颜色,只有不透明度足以使用户透明看不到字符)。
就是这样。
或者关注gif图片。
这是一个 UILabel 扩展,用于在同一行中显示连续的文本组件。
https://github.com/Umair-Bhatti/ConsecutiveComponents
只需添加文件和以下代码行
label.addConsectiveComponents(["indian ocean"])
它将始终在一行中显示 "indian ocean",即如果当前行中有 space,它将显示在下一行中。
我有一个多行 UILabel
,文本为“我想在印度洋乘船”。在 iPhone 5 中,由于宽度大小,UILabel
在第二行显示文本“ocean”。
我想要的是如果UILabel
中的文本出现在两行中,那么文本“indian ocean”应该显示在第二行或者宽度足够的情况下可用,所有文本应显示在一行中。
关于如何实现这一点的任何想法。
这是一个变通办法。但这适合您的要求,无需确定应用程序运行的设备。通常 UILabel
将单词包裹在 whitespaces
周围。因此,您可能应该将 indian ocean
设为一个单词,但它仍应显示为两个单独的单词。
let string = "<font size = 5 face = Avenir Next>I would like to ride a boat in<font color = white>_</font>indian ocean</font>"
let attributedString = try? NSAttributedString(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)
label.attributedText = attributedString
您可以指定所需的字体大小、系列并将 _
的颜色设置为 UILabel
的 backgroundColor
。
- 您应该计算 iPhone 宽度。
- 计算标签中的文本宽度。
- 如果文本宽度 > iPhone 宽度则在新行中设置 "indian ocean"
请检查以下代码:
let screenRect = UIScreen.mainScreen().bounds;
let screenWidth = screenRect.size.width;
let text:String = self.label.text!
let attr:NSDictionary = [NSFontAttributeName: self.label.font];
let textSize = (text as NSString).sizeWithAttributes(attr as? [String : AnyObject])
let textWidth = textSize.width
if textWidth > screenWidth{
self.label.text = "I would like to ride a boat in\nindian ocean"
}
label.numberOfLines = 0;
label.text = @"I would like to ride a boat in indian ocean";
[label sizeToFit];
无需使用设备检测...使用一个技巧即可解决此问题 "if label displays the text in two lines, then the text "indian ocean" 将显示第二行或显示在第一行。"
要遵循的步骤:
- 在属性检查器中将
UILabel
的文本类型从纯文本更改为属性。 - 在 "indian ocean" 之间输入任何字符,例如"indian_ocean".
- Select 那个字符(在我的例子中我使用下划线“_”)并更改文本颜色并将不透明度设置为 0%(不需要更改颜色,只有不透明度足以使用户透明看不到字符)。
就是这样。
或者关注gif图片。
这是一个 UILabel 扩展,用于在同一行中显示连续的文本组件。
https://github.com/Umair-Bhatti/ConsecutiveComponents
只需添加文件和以下代码行
label.addConsectiveComponents(["indian ocean"])
它将始终在一行中显示 "indian ocean",即如果当前行中有 space,它将显示在下一行中。