美文网首页
两个文本垂直中心点固定不变

两个文本垂直中心点固定不变

作者: NapoleonY | 来源:发表于2019-06-21 16:30 被阅读0次

    问题描述

    1.png

    如图所示,页面上下并排显示两条文本,文本动态变化,要求两条文本上下边之间的中心点(即图中蓝色箭头所指的位置)始终位于页面的 centerY 的位置。

    解决方案

    根据要显示的文本,生成富文本,两条文本的间距映射为富文本的段间距。然后设置 label.centerY.equalToSuperview()

            let str1 = "fakdjkaljfkldajfkdalkfda111\n"
            let str2 = "abcdfefkddakghdalkgj"
            let muAttStr1: NSMutableAttributedString = NSMutableAttributedString.init(string: str1)
            let paragraph1 = NSMutableParagraphStyle()
            paragraph1.paragraphSpacing = 10
            let dic = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 20),
                       NSAttributedString.Key.paragraphStyle:paragraph1,
                       NSAttributedString.Key.backgroundColor:UIColor.red]
            muAttStr1.addAttributes(dic, range: NSMakeRange(0, str1.count))
            
            let dic2 = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 10)]
            let muAttStr2: NSAttributedString = NSAttributedString.init(string: str2, attributes: dic2)
            
            muAttStr1.append(muAttStr2)
            desLabel.attributedText = muAttStr1.copy() as? NSAttributedString
            view.addSubview(desLabel)
            desLabel.snp.makeConstraints() { (make) in
                make.leading.equalToSuperview().offset(10)
                make.trailing.equalToSuperview().offset(-200)
                make.centerY.equalToSuperview()
            }
    

    相关文章

      网友评论

          本文标题:两个文本垂直中心点固定不变

          本文链接:https://www.haomeiwen.com/subject/jvquqctx.html