在 UIView 上绘制文本更好,还是使用 UILabel 并在 运行 时间更新文本?
Drawing text on UIView is better or using UILabel and updating text at run time?
请帮忙判断在UIView上绘制文字是使用了CPU多还是和我们使用UILabel一样,在运行时间(滚动时)更新里面的文字。例如。 gmail 通过姓名首字母显示用户个人资料图片的方式。
Most programs do some amount of drawing. If your program uses only
standard windows and controls, then you probably do not need to worry
too much about drawing performance. However, if you do any custom
drawing, you need to monitor your drawing code and make sure it is
performing at acceptable levels. In particular, if you support any of
the following, you should investigate ways to optimize your
drawing code.
- Live resizing
- Custom view drawing code, especially if portions of the view can be updated without updating the whole view
- Textured graphics
- Entirely opaque views
gmail 通过姓名首字母显示用户个人资料图片的方式可以使用 this library 轻松实现。
我建议使用 UILabel 而不是 UIView,因为它既是您的视图又是您的标签。
let yourLabel = UILabel(x0:, y:0, width:100, height:100)
yourLabel.backgroundColor = UIColor.red
yourLabel.textAligment = NSTextAligment.center
yourLabel.layer.cornerRadius = yourLabel.frame.size.widht/2
yourLabel.textColor = UIColor.white
yourLabel.text = "M"
self.view.addSubview(yourLabel)
label 更好,因为它更容易管理 UIView 为其添加一层复杂性的编辑。就性能而言,它可能没有太大区别。但就编码和管理而言,它有所不同。
最后还是要看你的应用需求了。
请帮忙判断在UIView上绘制文字是使用了CPU多还是和我们使用UILabel一样,在运行时间(滚动时)更新里面的文字。例如。 gmail 通过姓名首字母显示用户个人资料图片的方式。
Most programs do some amount of drawing. If your program uses only standard windows and controls, then you probably do not need to worry too much about drawing performance. However, if you do any custom drawing, you need to monitor your drawing code and make sure it is performing at acceptable levels. In particular, if you support any of the following, you should investigate ways to optimize your drawing code.
- Live resizing
- Custom view drawing code, especially if portions of the view can be updated without updating the whole view
- Textured graphics
- Entirely opaque views
gmail 通过姓名首字母显示用户个人资料图片的方式可以使用 this library 轻松实现。
我建议使用 UILabel 而不是 UIView,因为它既是您的视图又是您的标签。
let yourLabel = UILabel(x0:, y:0, width:100, height:100)
yourLabel.backgroundColor = UIColor.red
yourLabel.textAligment = NSTextAligment.center
yourLabel.layer.cornerRadius = yourLabel.frame.size.widht/2
yourLabel.textColor = UIColor.white
yourLabel.text = "M"
self.view.addSubview(yourLabel)
label 更好,因为它更容易管理 UIView 为其添加一层复杂性的编辑。就性能而言,它可能没有太大区别。但就编码和管理而言,它有所不同。
最后还是要看你的应用需求了。