如何制作文字换行符?

时间:2018-12-05 18:17:37

标签: ios swift uicollectionview

我正在尝试制作一个以3个图像和3个文本的可滚动collectionview开头的应用程序。但是我的descriptionText被iPhone的边缘剪掉了。

这是我的代码。怎么了?

import UIKit

class IntroAppCollectionViewCell: UICollectionViewCell {

    var data : MyData? {
        didSet{
            guard let unWrappedData = data else { return }

            imgFundo.image = UIImage(named: unWrappedData.imageURL)
            //descriptionText.text = unWrappedData.descriptionText

            let attributedText = NSMutableAttributedString(string: unWrappedData.title, attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18),NSAttributedString.Key.foregroundColor : UIColor.white])

            attributedText.append(NSAttributedString(string: "\n\n\n\(unWrappedData.descriptionText)", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15), NSAttributedString.Key.foregroundColor: UIColor.gray]))

            descriptionText.attributedText = attributedText
            descriptionText.textAlignment = .center

        }
    }

    let imgFundo : UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        img.translatesAutoresizingMaskIntoConstraints = false

        return img
    }()

    let descriptionText : UITextView = {
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.textColor = UIColor.white
        textView.textAlignment = .center
        textView.backgroundColor = UIColor.black
        textView.isEditable = false
        textView.isSelectable = false
        textView.isScrollEnabled = false

        return textView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = UIColor.black
        setupImg()
        setupText()
    }

    func setupText(){
        addSubview(descriptionText)

        descriptionText.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
        descriptionText.leftAnchor.constraint(equalTo: leftAnchor, constant: 10).isActive = true
        descriptionText.rightAnchor.constraint(equalTo: rightAnchor, constant: 10).isActive = true
        descriptionText.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -50).isActive = true

    }

    func setupImg(){
        addSubview(imgFundo)

        imgFundo.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor).isActive = true
        imgFundo.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor).isActive = true
        imgFundo.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor).isActive = true
        imgFundo.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor,constant: -150).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

1 个答案:

答案 0 :(得分:0)

更改您的setupText()功能

func setupText() {
    addSubview(descriptionText)

    descriptionText.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
    descriptionText.leftAnchor.constraint(equalTo: leftAnchor, constant: 10).isActive = true
    rightAnchor.constraint(equalTo: descriptionText.rightAnchor, constant: 10).isActive = true
    descriptionText.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -50).isActive = true

}