如何在 iPhone 应用程序中实现 UIButton/UILabel 'padding' [英] How to achieve UIButton / UILabel 'padding' in iPhone app

查看:16
本文介绍了如何在 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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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