簡體   English   中英

UILabel中的多行未顯示全文

[英]Multiple Lines in UILabel not showing full text

在我的代碼(Swift 2.0 iOS 9)中,我創建了一個UIScrollView並在ScrollView中添加了一個UILabel來顯示大量文本。 文本通過viewDidLoad()加載,並從包中的文件加載。

我使用了numberOfLines = 0label.lineBreakMode = .ByWordWrapping但只顯示了8行文字。 在代碼下方,您將看到代碼結果的圖像。 我給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
    }
}

這是代碼的結果: http//i.stack.imgur.com/Zh4En.png

我需要做什么才能顯示所有文本? 或者,是否有更好的方法來實現呈現信息文本的相同目標?

查看你的代碼,你調用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)

這會向標簽詢問具有給定寬度的尺寸,然后相應地設置框架。

另外在你的代碼中有autoresizing mask label.autoresizingMask = .FlexibleWidth沒有意義,因為寬度將與label.autoresizingMask = .FlexibleWidth一起改變,但高度不會,因此你的標簽的新大小將與它擁有的文本不匹配。

您需要“證明”您的文本,就像您在Microsoft Word中一樣!

您需要將NSMutableParagraphStyleNSAttributedString並行使用,以便將文本顯示為對齊。

必須將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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM