UILabel 中的多行不显示全文
Multiple Lines in UILabel not showing full text
在我的代码 (Swift 2.0 iOS 9) 中,我创建了一个 UIScrollView
并向 ScrollView 添加了一个 UILabel
以显示一个大集合文本。文本通过 viewDidLoad()
加载,并从包中的文件加载。
我用了numberOfLines = 0
和label.lineBreakMode = .ByWordWrapping
但是只显示了8行文字。在代码下方,您将看到代码结果的图像。我已经给 UILabel
和 UIScrollView
上色以显示它们的边界。
override func viewDidLoad() {
super.viewDidLoad()
self.configureView()
let sv = UIScrollView(frame: self.view.bounds)
sv.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.view.addSubview(sv)
sv.backgroundColor = UIColor.greenColor()
var y: CGFloat = 10
let file = detailItem as! String
if let sectionsFile = NSBundle.mainBundle().pathForResource(file, ofType: "txt") {
if let sectionText = try? String(contentsOfFile: sectionsFile, usedEncoding: nil) {
let label = UILabel()
label.numberOfLines = 0
label.text = sectionText
label.sizeToFit()
label.frame.origin = CGPointMake(10,y)
sv.addSubview(label)
y += label.bounds.size.height + 10
label.lineBreakMode = .ByWordWrapping
label.frame.size.width = self.view.bounds.size.width - 20
label.backgroundColor = UIColor.yellowColor()
label.autoresizingMask = .FlexibleWidth
}
var sz = sv.bounds.size
sz.height = y
sv.contentSize = sz
}
}
这是代码的结果:http://i.stack.imgur.com/Zh4En.png
我需要做什么才能显示所有文本?或者,有没有更好的方法来实现呈现信息文本的相同目标?
您需要'justify'您的文本,就像在 Microsoft Word 中一样!
您需要同时使用 NSMutableParagraphStyle
和 NSAttributedString
才能显示两端对齐的文本。
必须将 NSBaselineOffsetAttributedName 设置为 0.0。
示例代码
let sampleText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified
let attributedString = NSAttributedString(string: sampleText,
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 0
label.frame = CGRectMake(0, 0, 400, 400)
let view = UIView()
view.frame = CGRectMake(0, 0, 400, 400)
view.addSubview(label)
查看您的代码,您正在调用 sizeToFit
来设置标签本身的大小,但是您将标签框架的宽度设置为特定值的几行,而高度保持不变。新宽度可能小于 sizeToFit 调用期间标签给自己的宽度,这就是您的某些文本不适合的原因。
试试这个:
let desiredLabelWidth = self.view.bounds.size.width - 20
let size = label.sizeThatFits(CGSize(desiredLabelWidth, CGFloat.max))
label.frame = CGRect(x: 10, y: y, width: desiredLabelWidth, height: size.height)
这会询问标签的大小和给定的宽度,然后相应地设置框架。
同样在你的代码中有自动调整掩码 label.autoresizingMask = .FlexibleWidth
没有意义,因为宽度会随着超级视图一起改变,但高度不会,所以你的标签的新尺寸将与文本不匹配它有。
在我的代码 (Swift 2.0 iOS 9) 中,我创建了一个 UIScrollView
并向 ScrollView 添加了一个 UILabel
以显示一个大集合文本。文本通过 viewDidLoad()
加载,并从包中的文件加载。
我用了numberOfLines = 0
和label.lineBreakMode = .ByWordWrapping
但是只显示了8行文字。在代码下方,您将看到代码结果的图像。我已经给 UILabel
和 UIScrollView
上色以显示它们的边界。
override func viewDidLoad() {
super.viewDidLoad()
self.configureView()
let sv = UIScrollView(frame: self.view.bounds)
sv.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.view.addSubview(sv)
sv.backgroundColor = UIColor.greenColor()
var y: CGFloat = 10
let file = detailItem as! String
if let sectionsFile = NSBundle.mainBundle().pathForResource(file, ofType: "txt") {
if let sectionText = try? String(contentsOfFile: sectionsFile, usedEncoding: nil) {
let label = UILabel()
label.numberOfLines = 0
label.text = sectionText
label.sizeToFit()
label.frame.origin = CGPointMake(10,y)
sv.addSubview(label)
y += label.bounds.size.height + 10
label.lineBreakMode = .ByWordWrapping
label.frame.size.width = self.view.bounds.size.width - 20
label.backgroundColor = UIColor.yellowColor()
label.autoresizingMask = .FlexibleWidth
}
var sz = sv.bounds.size
sz.height = y
sv.contentSize = sz
}
}
这是代码的结果:http://i.stack.imgur.com/Zh4En.png
我需要做什么才能显示所有文本?或者,有没有更好的方法来实现呈现信息文本的相同目标?
您需要'justify'您的文本,就像在 Microsoft Word 中一样!
您需要同时使用 NSMutableParagraphStyle
和 NSAttributedString
才能显示两端对齐的文本。
必须将 NSBaselineOffsetAttributedName 设置为 0.0。
示例代码
let sampleText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified
let attributedString = NSAttributedString(string: sampleText,
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 0
label.frame = CGRectMake(0, 0, 400, 400)
let view = UIView()
view.frame = CGRectMake(0, 0, 400, 400)
view.addSubview(label)
查看您的代码,您正在调用 sizeToFit
来设置标签本身的大小,但是您将标签框架的宽度设置为特定值的几行,而高度保持不变。新宽度可能小于 sizeToFit 调用期间标签给自己的宽度,这就是您的某些文本不适合的原因。
试试这个:
let desiredLabelWidth = self.view.bounds.size.width - 20
let size = label.sizeThatFits(CGSize(desiredLabelWidth, CGFloat.max))
label.frame = CGRect(x: 10, y: y, width: desiredLabelWidth, height: size.height)
这会询问标签的大小和给定的宽度,然后相应地设置框架。
同样在你的代码中有自动调整掩码 label.autoresizingMask = .FlexibleWidth
没有意义,因为宽度会随着超级视图一起改变,但高度不会,所以你的标签的新尺寸将与文本不匹配它有。