界面

(IOS)让label的文字内容显示在左上/右上/左下/右下/中

2018-07-31  本文已影响103人  rightmost

自定义UILabel

// 重写label的textRectForBounds方法

-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {

    CGRectrect = [supertextRectForBounds:bounds limitedToNumberOfLines:numberOfLines];

    switch(self.textAlignmentType) {

        caseWZBTextAlignmentTypeLeftTop: {

            rect.origin = bounds.origin;

        }

            break;

        caseWZBTextAlignmentTypeRightTop: {

            rect.origin = CGPointMake(CGRectGetMaxX(bounds) - rect.size.width, bounds.origin.y);

        }

            break;

        caseWZBTextAlignmentTypeLeftBottom: {

            rect.origin = CGPointMake(bounds.origin.x, CGRectGetMaxY(bounds) - rect.size.height);

        }

            break;

        caseWZBTextAlignmentTypeRightBottom: {

            rect.origin = CGPointMake(CGRectGetMaxX(bounds) - rect.size.width, CGRectGetMaxY(bounds) - rect.size.height);

        }

            break;

        caseWZBTextAlignmentTypeTopCenter: {

            rect.origin = CGPointMake((CGRectGetWidth(bounds) - CGRectGetWidth(rect)) /2, CGRectGetMaxY(bounds) - rect.origin.y);

        }

            break;

        caseWZBTextAlignmentTypeBottomCenter: {

            rect.origin = CGPointMake((CGRectGetWidth(bounds) - CGRectGetWidth(rect)) /2, CGRectGetMaxY(bounds) - CGRectGetMaxY(bounds) - rect.size.height);

        }

            break;

        caseWZBTextAlignmentTypeLeft: {

            rect.origin = CGPointMake(0, rect.origin.y);

        }

            break;

        caseWZBTextAlignmentTypeRight: {

            rect.origin = CGPointMake(rect.origin.x,0);

        }

            break;

        caseWZBTextAlignmentTypeCenter: {

            rect.origin = CGPointMake((CGRectGetWidth(bounds) - CGRectGetWidth(rect)) /2, (CGRectGetHeight(bounds) - CGRectGetHeight(rect)) /2);

        }

            break;

        default:

            break;

    }

    returnrect;

}

- (void)drawTextInRect:(CGRect)rect {

    CGRecttextRect = [selftextRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];

    [superdrawTextInRect:textRect];

}

上一篇下一篇

猜你喜欢

热点阅读