使 UILabel 成 45 度,交叉背景色 swift [英] Make UILabel at 45 degree with a cross background colour swift

查看:31
本文介绍了使 UILabel 成 45 度,交叉背景色 swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以 45 度角显示带有背景颜色的文本.

I want to display a text at 45 degree with a background color.

示例

提前致谢

推荐答案

该元素包含两个子元素:

This element contains two sub-elements:

  1. 三角形 UIView;
  2. 旋转UILabel.

首先可以通过使用覆盖 draw 方法创建自定义视图来实现:

First can be achieved by creating custom view with override draw method:

class TriangleView: UIView {

    override func draw(_ rect: CGRect) {
        let path = UIBezierPath()
        path.move(to: .zero)
        path.addLine(to: CGPoint(x: rect.maxX, y: 0))
        path.addLine(to: CGPoint(x: 0, y: rect.maxY))
        path.close()

        UIColor.red.withAlphaComponent(0.5).setFill()
        path.fill()
    }

}

第二种是-45度的简单变换旋转:

The second is simple transform rotation for -45 degree:

label.transform = CGAffineTransform(rotationAngle: -.pi / 4)

结果:

接下来您应该管理 - 精确设置 UILabel 边界(标签不应离开屏幕)和行数.UILabel 元素的约束和属性检查器可以帮助您.

Next thing you should manage – precisely set UILabel bounds (label shouldn't go away from the screen) and lines count. Constraints and attributed inspector for UILabel element can help you.

这篇关于使 UILabel 成 45 度,交叉背景色 swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆