gpt4 book ai didi

ios - 我有一个仅当我的 UITableView 为空时才可见的 UILabel。如何在 Swift 中将该标签的文本居中?

转载 作者:可可西里 更新时间:2023-11-01 02:17:19 35 4
gpt4 key购买 nike

我的应用程序中有一个 UITableView,我想在没有内容时在那里显示一个 UILabel。到目前为止我正在做的:

@IBOutlet var tview: UITableView!

var emptyLabel = UILabel(frame: CGRectMake(0, 10, 100, 100))

override func viewDidLoad(){

let refreshControl = UIRefreshControl()

refreshControl.addTarget(self, action: "refresh:", forControlEvents: .ValueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "last updated on \(NSDate())")

tview.separatorStyle = UITableViewCellSeparatorStyle.None
tview.addSubview(refreshControl)


emptyLabel.numberOfLines = 0
emptyLabel.text = "There is no content in this table for now. Please pull down the list to refresh and something should appear"
emptyLabel.font = emptyLabel.font.fontWithSize(10)
tview.backgroundView = emptyLabel
}

但是当我这样做时,我得到以下结果:

enter image description here

而不是向左对齐,我想将文本居中,这样它看起来像:

       There is no content in this table for now. Please pull down the
list to refresh and something should appear

此外,目前它是垂直居中的 - 有没有办法将这条消息放在屏幕顶部的 1/3 处?

====== 编辑

@Md.Muzahidul Islam 这是我在空 table 时展示标签的方式:

override func tableView(tview: UITableView, numberOfRowsInSection section: Int) -> Int {

if self.items.count == 0{
self.emptyLabel.hidden = false
return 0
} else {
self.emptyLabel.hidden = true
return self.items.count;
}
}

最佳答案

您可以通过以下方式实现:

override func viewDidLoad()
{
// Other codes
emptyLabel = UILabel(frame: CGRectMake(0, 0, tview.bounds.size.width, view.bounds.size.height))
emptyLabel.textAlignment = NSTextAlignment.Center
emptyLabel.numberOfLines = 0
emptyLabel.text = "There is no content in this table for now. Please pull down the list to refresh and something should appear"
emptyLabel.font = emptyLabel.font.fontWithSize(10)
tview.backgroundView = emptyLabel
}

您可以阅读更多相关信息 here

关于ios - 我有一个仅当我的 UITableView 为空时才可见的 UILabel。如何在 Swift 中将该标签的文本居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36113437/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com