gpt4 book ai didi

ios - 如何在 Swift 中创建底部对齐的标签?

转载 作者:搜寻专家 更新时间:2023-11-01 06:05:25 24 4
gpt4 key购买 nike

有几个示例说明如何在 Swift 中创建顶部对齐标签。这是对我有用的:

@IBDesignable class TopAlignedLabel: UILabel {
override func drawTextInRect(rect: CGRect) {
if let stringText = text {
let stringTextAsNSString = stringText as NSString
var labelStringSize = stringTextAsNSString.boundingRectWithSize(CGSizeMake(CGRectGetWidth(self.frame), CGFloat.max),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: font],
context: nil).size
super.drawTextInRect(CGRectMake(0, 0, CGRectGetWidth(self.frame), ceil(labelStringSize.height)))
} else {
super.drawTextInRect(rect)
}
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
layer.borderWidth = 1
layer.borderColor = UIColor.blackColor().CGColor
}
}

我正在尝试修改它以创建一个底部对齐的标签,但很难找到要更改的正确内容。感谢您的帮助!

最佳答案

改变这一行:

super.drawTextInRect(CGRectMake(0, 0, CGRectGetWidth(self.frame), ceil(labelStringSize.height)))

进入这个:

super.drawTextInRect(CGRectMake(0, rect.size.height - labelStringSize.height, CGRectGetWidth(self.frame), ceil(labelStringSize.height)))

关于ios - 如何在 Swift 中创建底部对齐的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38793646/

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