簡體   English   中英

在UITableView中,自動布局中帶有填充的多行UILabel

[英]Multiline UILabel with padding in autolayout, in UITableView

我在UITableView中有一個UILabel,它最多應該有2行,並且周圍有一些填充(左右7個,頂部和底部2個)。 我正在使用自動布局,僅針對iOS6及更高版本。 正在以編程方式創建和添加所有視圖。

我已經將我的UILabel子類化了,這是init方法:

- (id)init
{
self = [super init];

self.translatesAutoresizingMaskIntoConstraints = NO;
self.numberOfLines = 2;
self.backgroundColor = UIColorFromARGB(0x99000000);
self.textColor = [UIColor whiteColor];
self.font = [UIFont boldSystemFontOfSize:14.0f];

return self;
}

如果我添加它,我得到正確的填充,但它只是一行:

- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {2, 7, 2, 7};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

我已經看過幾次這個答案,但它對我不起作用(沒有效果):

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
UIEdgeInsets insets = {2, 7, 2, 7};
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,insets) limitedToNumberOfLines:numberOfLines];
}

它是否在表視圖中有所不同? 任何幫助,將不勝感激。

正如你在上面的評論中所看到的,你並沒有真正說出你沒想到的是什么。 我剛剛完成了這個,這適用於autolayout:

@implementation InsetLabel

- (void) setInsets:(UIEdgeInsets)insets
    {
    _insets = insets ;
    [self invalidateIntrinsicContentSize] ;
    }

- (void)drawTextInRect:(CGRect)rect
    {
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
    }

- (void)resizeHeightToFitText
    {
    CGRect frame = [self bounds];
    CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);

    CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
    frame.size.height = newSize.height + self.insets.top + self.insets.bottom;
    self.frame = frame;
    }

- (CGSize) intrinsicContentSize
    {
    CGSize superSize = [super intrinsicContentSize] ;
    superSize.height += self.insets.top + self.insets.bottom ;
    superSize.width += self.insets.left + self.insets.right ;
    return superSize ;
    }

@end
@implementation InsetLabel

- (void) setInsets:(UIEdgeInsets)insets
    {
    _insets = insets ;
    [self invalidateIntrinsicContentSize] ;
    }

- (void)drawTextInRect:(CGRect)rect
    {
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
    }

- (void)resizeHeightToFitText
    {
    CGRect frame = [self frame];
    CGFloat textWidth = frame.size.width - (self.insets.left + self.insets.right);

    CGSize newSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(textWidth, 1000000) lineBreakMode:self.lineBreakMode];
    frame.size.height = newSize.height + self.insets.top + self.insets.bottom;
    self.frame = frame;
    }

- (CGSize) intrinsicContentSize
    {
    CGSize superSize = [super intrinsicContentSize] ;
    superSize.height += self.insets.top + self.insets.bottom ;
    superSize.width += self.insets.left + self.insets.right ;
    return superSize ;
    }
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self resizeHeightToFitText];
}

這工作甚至更好imho。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM