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'


基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01