UILabel textRectForBounds does not affect attempt to create a field

I am trying to defer text in UILabel to leave some margin around text displaying the background color. Following the suggestion here , I overtook textRectForBounds:limitedToNumberOfLines:as follows:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    CGRect intermediate = CGRectMake(bounds.origin.x+MARGIN,bounds.origin.y+MARGIN,bounds.size.width-2*MARGIN,bounds.size.height-2*MARGIN);
    return [super textRectForBounds:intermediate limitedToNumberOfLines:numberOfLines];
}

But no matter what I do, the text fits snugly against the left border of the rectangle. The picture seems to ignore the original part of the returned CGRect (although it seems to respect the part of the width, as if I had reduced the intermediate value to the width, for example bounds.size.width-200, the rect that returns textRectForBounds is correspondingly narrow and the text is drawn in a long, skinny column).

So: what else do I need to do to UILabelmake the drawing look like textForRectBounds-cased-rect origin.x and origin.y? I would prefer not to redefine UILabel drawTextInRectif I can help him.

Update : It was a long time ago, and I cannot remember exactly why another question did not work for me. I believe that this was because I tried to have UILabelmultiple lines, and the solution here did not work in this case.

+5
source share
2 answers

I think you should redefine both textRectForBounds:limitedToNumberOfLines:, and drawTextInRect:as follows:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    return CGRectInset(bounds, MARGIN, MARGIN);
}

- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)];
}
+8
source

, , . super , .

. , . numberOfLines, . , sizeToFit sizeThatFits: . , UITableViewCell , .

!

0

All Articles