这个问题在这里已经有了答案:
Best Answer-推荐答案 strong>
如果我是你,我会继承 UILabel
并添加 UIEdgeInsets
。在您的 UILabel
子类中执行以下操作:
.m 文件
- (id)initWithFrameCGRect)frame
{
self = [super initWithFrame:frame];
if (self){
//Property in the header file so we can add custom insets per instance of this class
self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}
return self;
}
-(void)drawTextInRectCGRect)rect
{
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
/* So that it will also work with auto layout */
-(CGSize)intrinsicContentSize
{
CGSize size = [super intrinsicContentSize];
size.width += self.edgeInsets.left + self.edgeInsets.right;
size.height += self.edgeInsets.top + self.edgeInsets.bottom;
if (self.numberOfLines == 0){
//There is a bug where intrinsice content
//size may be 1 point too short
size.height += 1;
}
return size;
}
关于ios - 使用自动布局或 layoutMargins 向 UILabel 添加填充?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/26879944/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) |
Powered by Discuz! X3.4 |