gpt4 book ai didi

ios - 中心 3 不均匀标签 - Interface Builder

转载 作者:行者123 更新时间:2023-11-28 23:22:19 26 4
gpt4 key购买 nike

附件是我尝试居中的图像以及我使用 XCode 11.2.1 所做的尝试 enter image description here

如何在 View 中将 3 个不同大小的标签(数字数据也将是动态的)水平居中?

我尝试过的事情:

我尝试在 LateBEDView(包含 3 个标签)上设置水平约束,但是当您在模拟器或实际设备上查看它时,它会将所有文本放在中心的右侧。我试过在两侧使用垫片(空 View )但无法弄清楚设置应该是什么?非常感谢任何帮助!

最佳答案

下面是我的做法。我会做这个 one 标签,毕竟它很容易居中。好的,我假设您知道该怎么做。

然后我将使用属性字符串来创建字符串的不同部分,包括“下标”。因此,首先我将扩展属性字符串键以包括三个部分:

extension NSAttributedString.Key {
static let part1 = NSAttributedString.Key(rawValue: "part1")
static let part2 = NSAttributedString.Key(rawValue: "part2")
static let part3 = NSAttributedString.Key(rawValue: "part3")
}

然后我会设置标签的属性文本:

let mas = NSMutableAttributedString()
mas.append(NSAttributedString(string: "Late BED = ", attributes: [
.font:UIFont.systemFont(ofSize: 15),
.foregroundColor:UIColor.black,
.part1:"part1"
]))
mas.append(NSAttributedString(string: "20.0", attributes: [
.font:UIFont.systemFont(ofSize: 15),
.foregroundColor:UIColor.black,
.part2:"part2"
]))
mas.append(NSAttributedString(string: " Gy(3.0)", attributes: [
.font:UIFont.systemFont(ofSize: 9),
.foregroundColor:UIColor.black,
.baselineOffset:-10,
.part3:"part3"
]))
self.label.attributedText = mas

结果看起来像你得到的,当然你可以根据需要调整它:

enter image description here

好的,这是真正聪明的部分。因为我划分了属性字符串的三个部分,所以我可以随意查找和更改其中任何一个的文本。正如您所说,每个数字的数据都需要能够更改。这就是你使用三个标签的原因!但现在我要展示如何使用一个标签来做到这一点。

例如,假设我想将“20.0”更改为“30.4”。那是你的第二个标签,也是我的 .part2。方法如下:

let s = self.label.attributedText
// skip past part 1 and find part 2
var r = NSRange()
let mas = s?.mutableCopy() as! NSMutableAttributedString
let _ = mas.attribute(.part2, at: 0, longestEffectiveRange: &r,
in: NSRange(location: 0, length: 100))
// find range of part 2
let _ = mas.attribute(.part2, at: r.length, longestEffectiveRange: &r,
in: NSRange(location: 0, length: 100))
mas.replaceCharacters(in: r, with: "30.4")
self.label.attributedText = mas

关于ios - 中心 3 不均匀标签 - Interface Builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462370/

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