How to achieve UIButton / UILabel #39;padding#39; in iPhone app(如何在 iPhone 应用程序中实现 UIButton/UILabel padding)
问题描述
我的 iPhone 应用程序中有各种需要填充的视图,例如文本左对齐的自定义 UIButton 和背景颜色的 UILabel.
I've got various views in my iPhone application that require padding e.g a custom UIButton with text aligned left, and a UILabel with a background color.
这可能是一个非常愚蠢的问题,但是如何应用填充"将文本移出左侧边缘?
This may be a really stupid question, but how can I apply 'padding' to move the text off the left hand edge?
我已经厌倦了使用边界等但没有任何成功.
I've tired using bounds etc without any success.
我知道我可以为我的 UILabel
创建一个带有背景颜色的包装视图,但这似乎有点过头了.
I know I could create a wrapper view for my UILabel
with a background color, but it seems like overkill.
非常感谢.
推荐答案
好吧,到目前为止我找到的最简单的解决方案是:
Ok the simplest solution I've found so far is:
self.textAlignment = UITextAlignmentCenter;
[self sizeToFit];
CGRect frame = self.frame;
frame.size.width += 20; //l + r padding
self.frame = frame;
不完全是我想要的,但它有效.
Not exactly what I wanted, but it works.
这篇关于如何在 iPhone 应用程序中实现 UIButton/UILabel 'padding'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 iPhone 应用程序中实现 UIButton/UILabel 'padding'


基础教程推荐
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01