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'
基础教程推荐
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
