简体   繁体   English

UILabel中的多行未显示全文

[英]Multiple Lines in UILabel not showing full text

In my code (Swift 2.0 iOS 9), I've created a UIScrollView and have added a UILabel to the ScrollView to show a large set of text. 在我的代码(Swift 2.0 iOS 9)中,我创建了一个UIScrollView并在ScrollView中添加了一个UILabel来显示大量文本。 The text is loaded through viewDidLoad() and is loaded from a file in the bundle. 文本通过viewDidLoad()加载,并从包中的文件加载。

I have used numberOfLines = 0 and label.lineBreakMode = .ByWordWrapping but only 8 lines of text show. 我使用了numberOfLines = 0label.lineBreakMode = .ByWordWrapping但只显示了8行文字。 Below the code, you will see a image of the result of the code. 在代码下方,您将看到代码结果的图像。 I have given color to the UILabel and UIScrollView to show their bounds. 我给UILabelUIScrollView提供了颜色以显示它们的界限。

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
    }
}

This is the results of the code: http://i.stack.imgur.com/Zh4En.png 这是代码的结果: http//i.stack.imgur.com/Zh4En.png

What do I need to do to allow all of the text to show? 我需要做什么才能显示所有文本? Alternatively, is there a better way to accomplish the same goal of presenting informational text? 或者,是否有更好的方法来实现呈现信息文本的相同目标?

Looking at your code, you are calling sizeToFit to make the label size itself, but few lines down you set width of your label's frame to a certain value, while the height stays the same. 查看你的代码,你调用sizeToFit来自己制作标签大小,但是你将标签框架的宽度设置为某个值,而高度保持不变,很少。 The new width is likely smaller than the one label gives itself during sizeToFit call, and that's the reason some of your text didin't fit. 在sizeToFit调用期间,新宽度可能小于一个标签给自己的宽度,这就是你的一些文本不合适的原因。

Try this: 尝试这个:

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)

This asks the label for it's size with a given width, and then sets the frame accordingly. 这会向标签询问具有给定宽度的尺寸,然后相应地设置框架。

Also in your code having autoresizing mask label.autoresizingMask = .FlexibleWidth doesn't make sense because the width will change together with the superview, but the height will not, so the new size of your label will not match the text it has. 另外在你的代码中有autoresizing mask label.autoresizingMask = .FlexibleWidth没有意义,因为宽度将与label.autoresizingMask = .FlexibleWidth一起改变,但高度不会,因此你的标签的新大小将与它拥有的文本不匹配。

You need to 'justify' your text, just like you would in Microsoft Word! 您需要“证明”您的文本,就像您在Microsoft Word中一样!

You need to use NSMutableParagraphStyle in parallel with an NSAttributedString in order to display text as justified. 您需要将NSMutableParagraphStyleNSAttributedString并行使用,以便将文本显示为对齐。

It's imperative to set NSBaselineOffsetAttributedName to 0.0. 必须将NSBaselineOffsetAttributedName设置为0.0。

Example code 示例代码

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM