UILabel text margin
来源:互联网 发布:h5 翻牌抽奖源码 编辑:程序博客网 时间:2024/06/06 18:08
Q:
I think I'm missing something really simple here but I'm looking to set the left inset/margin of a UILabel and can't seem to see a method to do so. The label has a background set so it would be ideal to inset the text by 10px or so on the left hand side. Any help would be appreciated.
A:
solved this by subclassing UILabel
and overriding drawTextInRect:
like this:
- (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];}
As you might have gathered, this is an adaptation of tc.'s answer. It has two advantages over that one:
- there's no need to trigger it by sending a
sizeToFit
message - it leaves the label frame alone - handy if your label has a background and you don't want that to shrink
A:
I think you should override both textRectForBounds:limitedToNumberOfLines:
anddrawTextInRect:
like this:
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines{ return CGRectInset(bounds, MARGIN, MARGIN);}- (void)drawTextInRect:(CGRect)rect{ [super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)];}
- UILabel text margin
- UILabel text margin
- UILabel.text截取方式
- iOS UILabel text两边对齐
- margin:auto与text-align:center
- iPhone – Wrap Text in UILabel
- UILabel的text顶部或底部显示
- 2.17 Displaying Static Text with UILabel
- iOS8 UILabel BUG ? text不显示
- 线程中更新 UILabel的text
- UILabel的text设置不同颜色、字体
- [转]css属性font text color background box margin border
- CSS属性总结(二):text, font, margin, padding
- css 属性总结(二):text, font, margin, padding
- margin:0 auto 和text-align:center的区别是什么???
- margin:0 auto 与 text-align:center 的区别
- margin:0 auto 与 text-align:center 的区别
- 从text-align和margin auto说开来
- 用IBM WebSphere DataStage进行数据整合: 第 1 部分
- 开启Objective-c的学习之旅(三)
- 浅析C#扩展方法
- 不为人知的关闭程序提示【xitongshoucang】
- BZOJ 1293 [SCOI2009] 生日礼物 题解与分析
- UILabel text margin
- hdu 4699 Editor 双栈维护最长前缀
- 创建线程
- RSA密钥的生成与配置
- |、|=、&、&=
- 比比看手机安全软件谁更强?【系统收藏】
- Session的生命周期
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- 取出数组中重复值,并获得重复个数【java】