UILabel只是用来现实文字,但是我们经常会遇到文字现实大小与Label大小不适应的问题,这个时候需要考虑换行了
我们直接看代码
let label1=UILabel(frame: CGRectMake(30,50,310,55)) label1.text="this determines the number of lines to draw and what to do when sizeToFit.Word Word Word." label1.backgroundColor=UIColor.grayColor() label1.numberOfLines=0 label1.lineBreakMode = NSLineBreakMode.ByWordWrapping//按照单词分割换行,保证换行时的单词完整。 self.view .addSubview(label1) let label2=UILabel(frame: CGRectMake(30,110,55)) label2.text="this determines the number of lines to draw and what to do when sizeToFit." label2.backgroundColor=UIColor.grayColor() label2.numberOfLines=0 label2.lineBreakMode = NSLineBreakMode.ByCharWrapping//按照字母换行,可能会在换行时将某个单词拆分到两行 self.view .addSubview(label2) let label4=UILabel(frame: CGRectMake(30,170,55)) label4.text="this determines the number of lines to draw and what to do when sizeToFit.Word Word Word." label4.backgroundColor=UIColor.grayColor() label4.numberOfLines=0 label4.lineBreakMode = NSLineBreakMode.ByClipping//按照单词换行,但是如果文字最后一行超过Label大小将直接超过边间现实不全 self.view .addSubview(label4) let label5=UILabel(frame: CGRectMake(30,230,55)) label5.text="this determines the number of lines to draw and what to do when sizeToFit.Word Word Word." label5.backgroundColor=UIColor.grayColor() label5.numberOfLines=0 label5.lineBreakMode = NSLineBreakMode.ByTruncatingHead//最后一行如果显示不全则在最后一行开始使用... self.view .addSubview(label5) let label6=UILabel(frame: CGRectMake(30,290,55)) label6.text="this determines the number of lines to draw and what to do when sizeToFit.Word Word Word." label6.backgroundColor=UIColor.grayColor() label6.numberOfLines=0 label6.lineBreakMode = NSLineBreakMode.ByTruncatingMiddle//最后一行如果显示不全则在最后一行中间使用... self.view .addSubview(label6) let label7=UILabel(frame: CGRectMake(30,350,55)) label7.text="this determines the number of lines to draw and what to do when sizeToFit.Word Word Word." label7.backgroundColor=UIColor.grayColor() label7.numberOfLines=0 label7.lineBreakMode = NSLineBreakMode.ByTruncatingTail//最后一行如果显示不全则在最后一行末尾使用... self.view .addSubview(label7)
NSLineBreakMode
这个枚举各个值的含义苹果开发群 :414319235 欢迎加入 欢迎讨论问题
原文链接:https://www.f2er.com/swift/326075.html