【问题标题】:iOS - Rounded rectangle around text in UITableViewCell like Things appiOS - UITableViewCell 中文本周围的圆角矩形,如 Things 应用程序
【发布时间】:2015-04-21 10:41:22
【问题描述】:

我搜索了高低,试图找出如何实现在 iOS 设置应用和 iPhone 的 Things 中看到的效果(下面是 CulturedCode 的应用)。

在 iOS 设置中,当有可用的操作系统更新时,单元格在最右侧的单元格中显示 1,但该数字被一个深灰色的圆角矩形包围。

Things for iPhone 应用在收件箱单元格中的以下屏幕截图中具有相似之处:

如何在 iOS 8 中使用 Swift 1.2 实现这种效果?

【问题讨论】:

  • 创建自定义 UIView。
  • 您可以从 Storyboard/XIB 本身实现圆角。签出this

标签: ios objective-c uitableview swift


【解决方案1】:

使用标签。设置字体。将文本颜色设置为白色:

numberLabel.textColor = UIColor.whiteColor()

设置背景颜色:

numberLabel.backgroundColor = UIColor.grayColor()

设置圆角半径:

numberLabel.layer.cornerRadius = 5

剪辑到边界:

numberLabel.clipsToBounds = true

【讨论】:

  • 完美!发挥了魅力!看到它在 iOS 设置应用程序中,我认为这可能是一个更标准的东西,但这也很容易!
  • 除了使用自定义视图外,我还没有看到其他方法来做到这一点。这样做 10 次左右就习惯了。
【解决方案2】:

基于这个答案:

objective C Rounded corners in a UITableView (*iOS7)

类似的代码应该适用于您的 UITableViewCell 上的 Swift 1.2:

cell.contentView.layer.cornerRadius = 5
cell.contentView.layer.masksToBounds = true

【讨论】:

    【解决方案3】:

    您可以为此使用 UILabel,调整其图层上的属性(一个 CALayer 实例,它执行标签的实际绘制)

    let badge = UILabel()
    // Give the badge some content and size, then:
    badge.layer.backgroundColor = UIColor.grayColor().CGColor;
    badge.layer.cornerRadius = badge.bounds.size.height * 0.5
    

    我过去曾多次使用这种方法。除了在代码中创建标签,您还可以将此方法与从 xib 或情节提要加载的标签一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多