1

UILabel があり、テキストのサイズを調整すると、UITextView のように見えるようにすることができますが、左余白が異なります。UIlabel では、UITextView にわずかな余白がある左の境界線に対してテキストが正対しています。これらのコントロールが互いの上に配置されたときに一貫して見えるように UILabel を調整するにはどうすればよいですか?

4

1 に答える 1

5

Simply change the label's frame:

CGRect frame = label.frame;
CGRect newFrame = CGRectMake(frame.origin.x + MARGIN, frame.origin.y, frame.size.width - MARGIN, frame.size.height);
label.frame = newFrame;

Of course replace MARGIN with whatever you want your margin to be.

Or you could subclass UILabel and override textRectForBounds:limitedToNumberOfLines: like so:

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

Hope this helps!

于 2010-06-24T05:23:30.980 に答える