美文网首页界面
(IOS)让label的文字内容显示在左上/右上/左下/右下/中

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

作者: rightmost | 来源:发表于2018-07-31 17:38 被阅读103次

    自定义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];

    }

    相关文章

      网友评论

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

        本文链接:https://www.haomeiwen.com/subject/gtghvftx.html