相关疑难解决方法(0)

添加空间/填充到UILabel

我有一个UILabel我想在顶部和底部添加空间的地方.在约束中使用最小高度我将其修改为:

在此输入图像描述

编辑:要做到这一点,我用过:

  override func drawTextInRect(rect: CGRect) {
        var insets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)
        super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))

    } 
Run Code Online (Sandbox Code Playgroud)

但我要找到一种不同的方法,因为如果我写两行以上的问题是相同的:

在此输入图像描述

padding uilabel ios

156
推荐指数
19
解决办法
20万
查看次数

UITextView/UILabel具有背景但行间距

我怎样才能在iOS中实现这种风格?

在此输入图像描述

我尝试使用NSAttributedString和设置NSBackgroundColorAttributeName为,blackColor但它没有clearColor行之间的空格.我也试着设置lineSpacingNSMutableParagraphStyle,但仍然,样式看起来像一个大黑块.

我只需要一个提示,以哪种方式去...谢谢!

换句话说,不是这样的:

在此输入图像描述

cocoa-touch uikit ios

6
推荐指数
2
解决办法
2896
查看次数

NSLayoutManager:如何在仅存在可渲染字形的情况下填充背景颜色

默认布局管理器填充没有文本(最后一行除外)的背景颜色(通过 NSAttributedString .backgroundColor 属性指定)。

在此输入图像描述

我已经通过子类化 NSLayoutManager 并覆盖来实现我想要的效果,func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint)如下所示:

override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
    guard let textContainer = textContainers.first, let textStorage = textStorage else { fatalError() }

    // This just takes the color of the first character assuming the entire container has the same background color.
    // To support ranges of different colours, you'll need to draw each glyph separately, querying the attributed string for the
    // background color attribute …
Run Code Online (Sandbox Code Playgroud)

nslayoutmanager core-text ios textkit swift

6
推荐指数
1
解决办法
2540
查看次数