【问题标题】:UILabel with subview带有子视图的 UILabel
【发布时间】:2011-09-30 06:34:23
【问题描述】:

由于某些原因,我必须有一个带有自定义背景视图的 UILabel,我想通过 [label addSubview:backgroundView] 来实现。

背景渲染良好,但标签文本没有出现。

我不能将背景颜色与图案图像一起使用。

【问题讨论】:

  • 您的问题是什么?你不能在上面使用UIImageViewUILabel 吗?
  • 问题是:为什么标签文本不显示,而子视图显示正确以及如何避免。我不能做的是改变设置。 UILabel 必须位于顶层,并且必须有一种方法可以在不使用 UIView 或图案背景的情况下获得背景视图
  • addSubview 在您的案例标签中添加当前视图,以便显示文本,只需关闭背景视图。加入关于之前cmets的一个UIImageView的问题
  • "addSubview 在当前视图上添加"表明将其发送到后台可以解决问题,但它没有。 ImageView 不是一个选项。
  • 将其发送到后台并不能解决问题,因为 backgroundView 是标签的单个子视图,而标签的文本位于其所有子视图的下方。你不会这样做,但如果你真的不想在某些东西上发布 UIlabel,你可以尝试在其上放置一个带有 superview 属性的表单:[label.superview insertSubview:view1 belowSubview:label];

标签: objective-c uilabel addsubview


【解决方案1】:

试试

[label insertSubview:backgroundView atIndex:0];

编辑:

我发现了一些不确定它会如何工作的东西。尝试使用 backgroundColor 属性。 比如:

label.backgroundColor = [UIColor colorWithPatternImage:yourImage];

太好了,我又迟到了……

【讨论】:

  • 你似乎很快就给出了负面评价。我搞砸了几次验证,当我终于成功时,安德里亚斯已经回答了。
  • 生活很艰难。也花费了我一个观点 - 但保持帖子干净。
【解决方案2】:

当我阅读了更多帖子时,我不得不重新考虑并最终使用 UIView 的子类。不像我的帖子和评论中所说的那样可取,但终于可以工作了。

【讨论】:

    【解决方案3】:

    确实是一个旧线程,但我遇到了同样的问题,并且认为我的解决方案可能会帮助某人(使用 PureLayout 和 swift 4)并以编程方式添加视图。

    viewController 中的示例用法:

     private lazy var labelContainer: UIView = {
            // Configure the label:
            let labelView = UILabel.newAutoLayout()
            labelView.text = label
            labelView.textColor = .red
            labelView.textAlignment = .center
            labelView.numberOfLines = 1
            
            // Configure your custom background view (UIView for this sample)
            let bgView = UIView.newAutoLayout()
            bgView.backgroundColor = .blue
            
            // The combined view with "padding":
            let paddingVertical = 12
            let paddingHorizontal = 16
            let padding = UIEdgeInsets.init(top: paddingVertical, left: paddingHorizontal, bottom: paddingVertical, right: paddingHorizontal)
            let view = labelView.wrapperOnTopOf(bgView: bgView, insets: padding)
            view.layer.cornerRadius = 15
            view.layer.masksToBounds = true
            return view
    }()
    

    现在您可以添加此视图并根据需要以编程方式设置约束。

    对于此实现,您需要 UIView 扩展 .wrapperOnTopOf,其实现如下:

    extension UIView {
       /**
        * Function to wrap the current view in a UIView on top of another view
        * - UIView
        */
       func wrapperOnTopOf(bgView: UIView, insets: UIEdgeInsets = UIEdgeInsets.zero) -> UIView {
           let container = UIView.newAutoLayout()
           bgView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
           container.insertSubview(bgView, at: 0)
           container.insertSubview(self, at: 1)
           // Let this view determine the size of the container:
           self.autoPinEdgesToSuperviewEdges(with: insets)
           return container
       }
    }
    

    【讨论】:

    • 我会升级这个,因为它使用了一种新技术以及 Swift。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 2012-01-28
    相关资源
    最近更新 更多