Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何居中标记文本顶部和底部空间_Swift_Uitableview_Uilabel_Uistackview - Fatal编程技术网

Swift 如何居中标记文本顶部和底部空间

Swift 如何居中标记文本顶部和底部空间,swift,uitableview,uilabel,uistackview,Swift,Uitableview,Uilabel,Uistackview,我在tableview单元格的内容视图的stackview中有一个UILabel。一切正常,单元格正在调整大小,但我无法将ui标签中的文本居中。我总是在文章的底部有奇怪的空格。查看屏幕截图 黄色:内容视图 绿色:stackview 蓝色:标签 其他信息:单元格没有静态高度。黄色视图具有上下、尾随和前导约束,加上与超级视图对齐的水平和垂直约束,与堆栈视图相同。我通过代码激活和禁用的唯一具有恒定高度的东西是按钮。所以它根据单元格隐藏和显示。Stackview按比例填充 ui标签是sizeToFit

我在tableview单元格的内容视图的stackview中有一个
UILabel
。一切正常,单元格正在调整大小,但我无法将
ui标签中的文本居中。我总是在文章的底部有奇怪的空格。查看屏幕截图

黄色:内容视图 绿色:stackview 蓝色:标签

其他信息:单元格没有静态高度。黄色视图具有上下、尾随和前导约束,加上与超级视图对齐的水平和垂直约束,与堆栈视图相同。我通过代码激活和禁用的唯一具有恒定高度的东西是按钮。所以它根据单元格隐藏和显示。Stackview按比例填充

ui标签是
sizeToFit

还尝试:

detailLbl.textAlignment = .center
detailLbl.lineBreakMode = .byWordWrapping //also tried byClipping
detailLbl.baselineAdjustment = .alignCenters
我将里面的文本解析为HTML

"<p><font color=\"#cd1719\">V/. </font>…The Word of the Lord.<br><b></b><font color=\"#cd1719\">R/. </font><b>Thanks be to God.</b></p>"
这就是我在代码中使用它的方式

if let attributedString = try? NSAttributedString(htmlString: htmlTxt, font: UIFont(name: "Avenir-Roman", size: CGFloat(Constants.defualtContentDescriptionFontSize)), useDocumentFontSize: false) {
        cell.detailLbl.attributedText = attributedString
}

部分问题在于您使用的是格式化的html文本,其中包含“额外空间”

例如,您的第一个html文本行包括

标记

此代码创建两个绿色背景标签,每个标签的宽度相同,且没有高度约束:

class ViewController: UIViewController, UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let firstLabel = UILabel()
        firstLabel.backgroundColor = .green
        firstLabel.numberOfLines = 0
        firstLabel.translatesAutoresizingMaskIntoConstraints = false

        let secondLabel = UILabel()
        secondLabel.backgroundColor = .green
        secondLabel.numberOfLines = 0
        secondLabel.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(firstLabel)
        view.addSubview(secondLabel)

        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            
            // first label 40-pts from top, 20-pts on each side
            firstLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
            firstLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            firstLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),

            // second label 40-pts from bottom of first label, 20-pts on each side
            secondLabel.topAnchor.constraint(equalTo: firstLabel.bottomAnchor, constant: 40.0),
            secondLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            secondLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            
        ])
        
        var htmlTxt = "<p><font color=\"#cd1719\">V/. </font>…The Word of the Lord.<br><b></b><font color=\"#cd1719\">R/. </font><b>Thanks be to God.</b></p>"
        
        if let attributedString = try? NSAttributedString(htmlString: htmlTxt, font: UIFont(name: "Avenir-Roman", size: 24.0), useDocumentFontSize: false) {
            firstLabel.attributedText = attributedString
            //cell.detailLbl.attributedText = attributedString
        }

        htmlTxt = "<font color=\"#cd1719\">V/. </font>…The Word of the Lord.<br><b></b><font color=\"#cd1719\">R/. </font><b>Thanks be to God.</b>"
        
        if let attributedString = try? NSAttributedString(htmlString: htmlTxt, font: UIFont(name: "Avenir-Roman", size: 24.0), useDocumentFontSize: false) {
            secondLabel.attributedText = attributedString
            //cell.detailLbl.attributedText = attributedString
        }
    }

}
类ViewController:UIViewController、UIScrollViewDelegate{
重写func viewDidLoad(){
super.viewDidLoad()
设firstLabel=UILabel()
firstLabel.backgroundColor=.green
firstLabel.numberOfLines=0
firstLabel.TranslatesAutoResizezingMaskintoConstraints=false
让secondLabel=UILabel()
secondLabel.backgroundColor=.green
secondLabel.numberOfLines=0
secondLabel.TranslatesAutoResizezingMaskintoConstraints=false
view.addSubview(第一个标签)
view.addSubview(第二个标签)
设g=视图。安全区域布局指南
NSLayoutConstraint.activate([
//第一个标签距顶部40分,每侧20分
firstLabel.topAnchor.constraint(等式:g.topAnchor,常数:40.0),
firstLabel.leadingAnchor.constraint(等式:g.leadingAnchor,常数:20.0),
firstLabel.trailingAnchor.constraint(等式:g.trailingAnchor,常数:-20.0),
//第二个标签距第一个标签底部40分,每侧20分
secondLabel.topAnchor.constraint(等式:firstLabel.bottomAnchor,常量:40.0),
第二个Label.leadingAnchor.constraint(等式:g.leadingAnchor,常数:20.0),
secondLabel.trailingAnchor.constraint(等式:g.trailingAnchor,常数:-20.0),
])
var htmlText=“V/…主的话。
R/。感谢上帝。

” 如果让AttributeString=try?nsAttributeString(htmlString:htmlTxt,字体:UIFont(名称:“Avenir Roman”,大小:24.0),则使用DocumentFontSize:false){ firstLabel.attributedText=attributedString //cell.detailLbl.attributedText=attributedString } htmlTxt=“V/…耶和华的话。
R/。感谢上帝。” 如果让AttributeString=try?nsAttributeString(htmlString:htmlTxt,字体:UIFont(名称:“Avenir Roman”,大小:24.0),则使用DocumentFontSize:false){ secondLabel.attributedText=attributedString //cell.detailLbl.attributedText=attributedString } } }
输出:

两者的区别是什么?对于第二个标签,在使用扩展处理html之前,我删除了

标记

您的第二个html文本将其定义为
h6
。。。以下是该字符串的输出:

同样,两者之间的区别在于我在处理html之前删除了
h6
标记


您可能需要修改扩展名以删除段落标记,或者设置一个段落样式以忽略它们。

显示约束代码?你为桌子设置了静态高度吗?@shu Khan没有静态高度。黄色视图具有上下、尾随和前导约束,加上与超级视图对齐的水平和垂直约束,与堆栈视图相同。我通过代码激活和禁用的唯一具有恒定高度的东西是按钮。所以它根据单元格隐藏和显示。Stackview正在填充proportionally@KeghamK. - 不要按比例使用填充。。。使用
填充
。不要更改按钮的高度约束。设置按钮的
.ishiden
属性以显示/隐藏它。隐藏时,堆栈视图将自动删除它占用的空间。@DonMag也尝试过了不,它不起作用。是无需更改隐藏的约束就足够了,但在bottom@KeghamK. - 显示用于解析html的代码。试着从主视图中的
UILabel
开始——忘记将其嵌入到表视图单元格的堆栈视图中。谢谢,亲爱的,这是正确的答案。我还删除了stackview。我将用和空字符串替换出现的和

标记,我相信它会被调整
if let attributedString = try? NSAttributedString(htmlString: htmlTxt, font: UIFont(name: "Avenir-Roman", size: CGFloat(Constants.defualtContentDescriptionFontSize)), useDocumentFontSize: false) {
        cell.detailLbl.attributedText = attributedString
}
class ViewController: UIViewController, UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let firstLabel = UILabel()
        firstLabel.backgroundColor = .green
        firstLabel.numberOfLines = 0
        firstLabel.translatesAutoresizingMaskIntoConstraints = false

        let secondLabel = UILabel()
        secondLabel.backgroundColor = .green
        secondLabel.numberOfLines = 0
        secondLabel.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(firstLabel)
        view.addSubview(secondLabel)

        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            
            // first label 40-pts from top, 20-pts on each side
            firstLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
            firstLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            firstLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),

            // second label 40-pts from bottom of first label, 20-pts on each side
            secondLabel.topAnchor.constraint(equalTo: firstLabel.bottomAnchor, constant: 40.0),
            secondLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            secondLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            
        ])
        
        var htmlTxt = "<p><font color=\"#cd1719\">V/. </font>…The Word of the Lord.<br><b></b><font color=\"#cd1719\">R/. </font><b>Thanks be to God.</b></p>"
        
        if let attributedString = try? NSAttributedString(htmlString: htmlTxt, font: UIFont(name: "Avenir-Roman", size: 24.0), useDocumentFontSize: false) {
            firstLabel.attributedText = attributedString
            //cell.detailLbl.attributedText = attributedString
        }

        htmlTxt = "<font color=\"#cd1719\">V/. </font>…The Word of the Lord.<br><b></b><font color=\"#cd1719\">R/. </font><b>Thanks be to God.</b>"
        
        if let attributedString = try? NSAttributedString(htmlString: htmlTxt, font: UIFont(name: "Avenir-Roman", size: 24.0), useDocumentFontSize: false) {
            secondLabel.attributedText = attributedString
            //cell.detailLbl.attributedText = attributedString
        }
    }

}