删除Uilabel顶部和底部的自由空间

发布于 2025-02-03 14:05:20 字数 2192 浏览 2 评论 0原文

大家好。我正在尝试找到一种方法,以删除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)
    }
}

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文