简体   繁体   English

Swift:如何在使用AutoLayout时在UITextView中调整字体大小?

[英]Swift: How to resize the font size within a UITextView while using AutoLayout?

I am one week into Swift programing and I want to build my first Application with Autolayout. 我花了一周的时间进行Swift编程,我想用Autolayout构建我的第一个应用程序。 The current state of my app is that I generate a bunch of PictureCell in my ViewController. 我的应用程序的当前状态是,我在ViewController中生成了一堆PictureCell。 Their size is based on a slider value (and also calculated in the ViewController). 它们的大小基于滑块值(也可以在ViewController中计算)。 This works just fine. 这样很好。

My struggle is customizing the inside of my PictureCell. 我的工作是自定义PictureCell的内部。 My goal is to have a Label in the cell which font size is automatically resized when I resize the cell. 我的目标是在单元格中有一个Label,当我调整单元格的大小时,字体大小会自动调整。

At the current state I can resize the Cell and the UITextView like I want, but I cannot resize the font within the Textview because it's constant is just called when it is initialized (I guess). 在当前状态下,我可以根据需要调整Cell和UITextView的大小,但是我无法在Textview中调整字体的大小,因为它的常数在初始化时才被调用(我猜)。

How can I address this problem in a good way? 我怎样才能很好地解决这个问题?

Due to a not understanding of Swifts logic I have to post the whole code of the PictureCell: 由于不了解Swifts逻辑,因此我不得不发布PictureCell的整个代码:

class PictureCell: UICollectionViewCell {
override init(frame: CGRect) {
    super.init(frame: frame)
    self.translatesAutoresizingMaskIntoConstraints = false
    self.layer.cornerRadius = self.bounds.width / 20
    self.clipsToBounds = true

    setupViews()
}

let descriptionTextView: UITextView = {
    let textView = UITextView()
    textView.text = "Header"
    textView.textColor = .black
    textView.backgroundColor = .white
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.textAlignment = .center
    textView.isEditable = false
    textView.isScrollEnabled = false
    textView.sizeToFit()
    textView.font = UIFont.boldSystemFont(ofSize: textView.contentSize.height / 2) // Resize that

    textView.layer.borderWidth = 2
    textView.layer.borderColor = UIColor.red.cgColor

    return textView
}()


var mainPicture: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    imageView.clipsToBounds = true

    return imageView
}()

 func setPictureForIndex(index: Int)  {
    self.mainPicture.image = UIImage(named: "color\(index)")
}

func setupViews() {
    addSubview(mainPicture)
    confMainPicture()

    addSubview(descriptionTextView)
    confDescriptionTextView()
}

func confMainPicture() {
    mainPicture.translatesAutoresizingMaskIntoConstraints = false
    mainPicture.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    mainPicture.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
    mainPicture.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    mainPicture.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}

func confDescriptionTextView(){
    descriptionTextView.translatesAutoresizingMaskIntoConstraints = false
    descriptionTextView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    descriptionTextView.heightAnchor.constraint(equalTo: mainPicture.heightAnchor, multiplier: 0.25).isActive = true
    descriptionTextView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
}

Too small for the text 文字太小

Just fine 正好

Too big to look good 太大看起来不好

This Code solved my problem more or less: It doesn't work properly if the Cell is really small but it's better than the starting point and maybe someone can use it. 这段代码或多或少地解决了我的问题:如果Cell真的很小,它就无法正常工作,但是它比起点更好,也许有人可以使用它。

class PictureCell: UICollectionViewCell {
override init(frame: CGRect) {
    super.init(frame: frame)
    self.translatesAutoresizingMaskIntoConstraints = false
    self.layer.cornerRadius = self.bounds.width / 20
    self.clipsToBounds = true
    setupViews()
}
//MARK: -

var cellIdetifier = Int()

var mainPicture: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    imageView.clipsToBounds = true
    imageView.layer.cornerRadius = imageView.bounds.width / 20
    return imageView
}()

var descriptionBox: UIView = {
    let descVie = UIView()
    descVie.backgroundColor = UIColor(red: 0.1 , green: 0.1, blue: 0.1, alpha: 0.5)
    descVie.layer.borderWidth = 0.5
    descVie.layer.borderColor = UIColor.black.cgColor
    descVie.clipsToBounds = true
    descVie.layer.cornerRadius = descVie.bounds.height / 5
    return descVie
}()

lazy var descLabel: UITextField = {
    let label = UITextField()
    label.textColor = .white
    label.textAlignment = .center
    label.clipsToBounds = true
    label.font = UIFont.systemFont(ofSize: 15)
    label.adjustsFontSizeToFitWidth = true
    label.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    label.sizeToFit()
    label.layoutIfNeeded()
    label.isUserInteractionEnabled = false
    return label
}()

func setPictureForIndex(index: Int, name: String)  {
    self.descLabel.text = name
    self.mainPicture.image = UIImage(named: "color\(index)")
}
// MARK: -
// MARK: Layout
func setupViews() {
    addSubview(mainPicture)
    addSubview(descriptionBox)
    descriptionBox.addSubview(descLabel)
    confBounds()
}

func confBounds() {
    mainPicture.translatesAutoresizingMaskIntoConstraints = false
    mainPicture.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    mainPicture.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
    mainPicture.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    mainPicture.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

    descriptionBox.translatesAutoresizingMaskIntoConstraints = false
    descriptionBox.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    descriptionBox.heightAnchor.constraint(equalTo: mainPicture.heightAnchor, multiplier: 0.25).isActive = true
    descriptionBox.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    descriptionBox.bottomAnchor.constraint(greaterThanOrEqualTo: mainPicture.topAnchor, constant: 1)

    descLabel.translatesAutoresizingMaskIntoConstraints = false
    descLabel.widthAnchor.constraint(equalTo: descriptionBox.widthAnchor).isActive = true
    descLabel.heightAnchor.constraint(equalTo: descriptionBox.heightAnchor).isActive = true
    descLabel.bottomAnchor.constraint(equalTo: descriptionBox.bottomAnchor).isActive = true
    descLabel.heightAnchor.constraint(equalTo: descriptionBox.heightAnchor).isActive = true
}

} }

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

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