【问题标题】:How to Increase/Decrease the UILabel font size along with width and height while scrolling in iOS Swift4如何在 iOS Swift4 中滚动时增加/减少 UILabel 字体大小以及宽度和高度
【发布时间】:2017-12-15 12:10:16
【问题描述】:

我有一个 ViewController(MainVC),它包含一个标签 (myLabel) 和一个表格视图 (myTableView)。

每当我滚动 myTableView 时,我需要自动增加/减少 UILabel 字体大小以及宽度和高度。

例子:-

最初 myLabel.numberoflines = 2, myLabel.font = 30,宽度 = 100,高度 = 100。

现在,当我开始滚动 myTableView 时,它看起来像 myLabel.numberoflines = 1,myLabel.font = 20,宽度 = 200,高度 = 50。

在我的代码中 -UIViewController --UILabel(子视图1) --UITableView(subview2)

我已经尝试过这样的

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        myLabel.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}

XCode 9.2、Swift 4、iOS 11.2

【问题讨论】:

  • 试试这个myLabel.frame = CGRect(x: z, y: z, width: z, height: z)myLabel.numberOfLines = x
  • 您能告诉我们更多细节吗?就像你想如何触发转换一样。并且您希望标签在两种大小之间变化,或者在向下滚动 tableview 时保持增加,而在向上滚动时保持减小。
  • @RockBalbao 感谢您的回复。 myLabel.frame = CGRect(x: z, y: z, width: z, height: z) myLabel.numberOfLines = x. Z值如何计算?
  • @JsW 感谢您的回复。只需在我们向上滚动时减小 myLabel 字体大小和 numberoflines 为 1,在我们向下滚动时增加 myLabel 字体大小和 numberoflines 为 2。

标签: ios swift uitableview


【解决方案1】:

如果您已经初始化了表格视图,请将UITableViewDelegate 添加到您的控制器中。
它继承自UIScrollViewDelegate。我们将在UIScrollViewDelegate中使用两种方法。

extension ViewController: UITableViewDelegate {

    // mark where you begin to drag
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        dragOriginY = scrollView.contentOffset.y
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // compare your start point with the current offset. 
        if dragOriginY - scrollView.contentOffset.y < 0 {
                // the drag direction is up
                self.titleLabel.numberOfLines = 2
                self.titleLabel.font = UIFont.systemFont(ofSize: 23)

        } else {
            // drag downwards
            titleLabel.numberOfLines = 1
            titleLabel.font = UIFont.systemFont(ofSize: 17)
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2016-07-12
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多