删除Uilabel顶部和底部的自由空间
大家好。我正在尝试找到一种方法,以删除Uilabel中文本上方和下方的空白(在屏幕截图中盘旋)。我谷歌搜索了很多,但我不知道。感谢您的帮助!
正如@Sweeper所问的那样,我附加了代码
@IBDesignable final class LabelWithoutPadding: UILabel {
override func drawText(in rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
// At the first, i thought that it was insets, but this did not help at all
// So this code doesn't fix my problem
let insets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
super.drawText(in: rect.inset(by: insets))
return
}
// Source https://stackoverflow.com/questions/35335068/how-to-use-coretext-to-replace-uilabel-in-swift
context.textMatrix = CGAffineTransform.identity;
// You can see height - 4
/*
It helped, but only for the bottom.
And it seems to me that this is a completely wrong decision.
Also, some labels stopped showing text because of this and the code below,
so I think that the solution has not been found.
*/
context.translateBy(x: 0, y: self.bounds.size.height - 4);
context.scaleBy(x: 1.0, y: -1.0);
let path = CGMutablePath()
path.addRect(self.bounds)
let str = NSMutableAttributedString(string: self.text ?? "")
// set font color
str.addAttribute(NSAttributedString.Key(rawValue: kCTForegroundColorAttributeName as String), value: self.textColor ?? Colors.contrastHighter , range: NSMakeRange(0, str.length))
// set font name & size
let fontRef = self.font ?? FontMaker.getFont(fontSize: 12, fontWeight: 400)
str.addAttribute(NSAttributedString.Key(rawValue: kCTFontAttributeName as String), value: fontRef, range:NSMakeRange(0, str.length))
let frameSetter = CTFramesetterCreateWithAttributedString(str)
let ctFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, str.length), path, nil)
CTFrameDraw(ctFrame, context)
}
}
Hi all. I'm trying to find a way to remove the white space (circled in the screenshot) above and below the text in a UILabel. I googled a lot, but i can't figure out. Thanks for help!
As @Sweeper asks, i attaching my code
@IBDesignable final class LabelWithoutPadding: UILabel {
override func drawText(in rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
// At the first, i thought that it was insets, but this did not help at all
// So this code doesn't fix my problem
let insets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
super.drawText(in: rect.inset(by: insets))
return
}
// Source https://stackoverflow.com/questions/35335068/how-to-use-coretext-to-replace-uilabel-in-swift
context.textMatrix = CGAffineTransform.identity;
// You can see height - 4
/*
It helped, but only for the bottom.
And it seems to me that this is a completely wrong decision.
Also, some labels stopped showing text because of this and the code below,
so I think that the solution has not been found.
*/
context.translateBy(x: 0, y: self.bounds.size.height - 4);
context.scaleBy(x: 1.0, y: -1.0);
let path = CGMutablePath()
path.addRect(self.bounds)
let str = NSMutableAttributedString(string: self.text ?? "")
// set font color
str.addAttribute(NSAttributedString.Key(rawValue: kCTForegroundColorAttributeName as String), value: self.textColor ?? Colors.contrastHighter , range: NSMakeRange(0, str.length))
// set font name & size
let fontRef = self.font ?? FontMaker.getFont(fontSize: 12, fontWeight: 400)
str.addAttribute(NSAttributedString.Key(rawValue: kCTFontAttributeName as String), value: fontRef, range:NSMakeRange(0, str.length))
let frameSetter = CTFramesetterCreateWithAttributedString(str)
let ctFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, str.length), path, nil)
CTFrameDraw(ctFrame, context)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论