iOS的UILabel設定居上對齊,置中對齊,居下對齊

來源:互聯網
上載者:User

標籤:pre   object   cal   set   cti   位置   wrap   regexp   interface   

前言:


沒有理由不去努力.png

本文:
想實現UILabel居上對齊,置中對齊,居下對齊,如下效果:


.png在iOS中預設的UILabel中的文字在豎直方向上只能置中對齊,博主參考國外網站,從UILabel繼承了一個新類,實現了居上對齊,置中對齊,居下對齊

具體如下:

建立:MYLabel 繼承與UILabel

在MYLabel.h中完成

在MYLabel.h中完成

////  MYLabel.h//  LabelDemo////  Created by wangergang on 2016/12/7.//  Copyright ? 2016年 MYCompangName. All rights reserved.//#import <UIKit/UIKit.h>typedef enum {    VerticalAlignmentTop = 0, //default    VerticalAlignmentMiddle,    VerticalAlignmentBottom,} VerticalAlignment;@interface MYLabel : UILabel {@private    VerticalAlignment _verticalAlignment;}@property (nonatomic) VerticalAlignment verticalAlignment;@end

在MYLabel.m中完成

在MYLabel.m中完成

////  MYLabel.m//  LabelDemo////  Created by wangergang on 2016/12/7.//  Copyright ? 2016年 MYCompangName. All rights reserved.//#import "MYLabel.h"@implementation MYLabel@synthesize verticalAlignment = verticalAlignment_;- (id)initWithFrame:(CGRect)frame {    self = [super initWithFrame:frame];    if (self) {        self.verticalAlignment = VerticalAlignmentMiddle;    }    return self;}- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {    verticalAlignment_ = verticalAlignment;    [self setNeedsLayout];}- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];    switch (self.verticalAlignment) {        case VerticalAlignmentTop:            textRect.origin.y = bounds.origin.y;            break;        case VerticalAlignmentBottom:            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;            break;        case VerticalAlignmentMiddle:            // Fall through.        default:            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;    }    return textRect;}-(void)drawTextInRect:(CGRect)requestedRect {    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];    [super drawTextInRect:actualRect];}@end

使用:首先記得引入標頭檔

import "MYLabel.h"
- (void)viewDidLoad {    [super viewDidLoad];    [self setupLabel];    // Do any additional setup after loading the view, typically from a nib.}- (void)setupLabel {    //居上對齊    MYLabel *topLabel = [[MYLabel alloc] initWithFrame:CGRectMake(20, 275, 350, 200)];    topLabel.text = @"劇終了、劇終了、劇終了、劇終了、劇終了、劇終了、劇終了、";    topLabel.backgroundColor = [UIColor cyanColor];    topLabel.textAlignment = NSTextAlignmentLeft;    topLabel.textColor = [UIColor blueColor];    topLabel.lineBreakMode = NSLineBreakByCharWrapping;    topLabel.numberOfLines = 0;    [topLabel setVerticalAlignment:VerticalAlignmentMiddle];    [self.view addSubview:topLabel];    //置中對齊    MYLabel *middleLabel = [[MYLabel alloc] initWithFrame:CGRectMake(20, 500, 350, 200)];    middleLabel.text = @"向下看、向下看、向下看、向下看、向下看、向下看、向下看、向下看、";    middleLabel.backgroundColor = [UIColor cyanColor];    middleLabel.textAlignment = NSTextAlignmentLeft;    middleLabel.textColor = [UIColor blueColor];    middleLabel.lineBreakMode = NSLineBreakByCharWrapping;    middleLabel.numberOfLines = 0;    [middleLabel setVerticalAlignment:VerticalAlignmentBottom];    [self.view addSubview:middleLabel];    //居下對齊    MYLabel *bottomLabel = [[MYLabel alloc] initWithFrame:CGRectMake(20, 50, 350, 200)];    bottomLabel.text = @"看我居上對齊了啊、你看看對不對的啊、看來是對的";    bottomLabel.backgroundColor = [UIColor cyanColor];    bottomLabel.textAlignment = NSTextAlignmentLeft;    bottomLabel.textColor = [UIColor blueColor];    bottomLabel.lineBreakMode = NSLineBreakByCharWrapping;    bottomLabel.numberOfLines = 0;    [bottomLabel setVerticalAlignment:VerticalAlignmentTop];    [self.view addSubview:bottomLabel];}

其如:就不再上傳了

Demo位置github位置

iOS的UILabel設定居上對齊,置中對齊,居下對齊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.