前言:
正文:
想實(shí)現(xiàn)UILabel居上對(duì)齊旁舰,居中對(duì)齊锋华,居下對(duì)齊,如下效果:
在iOS中默認(rèn)的UILabel中的文字在豎直方向上只能居中對(duì)齊鬓梅,博主參考國(guó)外網(wǎng)站供置,從UILabel繼承了一個(gè)新類,實(shí)現(xiàn)了居上對(duì)齊绽快,居中對(duì)齊芥丧,居下對(duì)齊
具體如下:
創(chuàng)建: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 {
//居上對(duì)齊
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];
//居中對(duì)齊
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];
//居下對(duì)齊
MYLabel *bottomLabel = [[MYLabel alloc] initWithFrame:CGRectMake(20, 50, 350, 200)];
bottomLabel.text = @"看我居上對(duì)齊了啊蜀铲、你看看對(duì)不對(duì)的啊、看來(lái)是對(duì)的";
bottomLabel.backgroundColor = [UIColor cyanColor];
bottomLabel.textAlignment = NSTextAlignmentLeft;
bottomLabel.textColor = [UIColor blueColor];
bottomLabel.lineBreakMode = NSLineBreakByCharWrapping;
bottomLabel.numberOfLines = 0;
[bottomLabel setVerticalAlignment:VerticalAlignmentTop];
[self.view addSubview:bottomLabel];
}
swift 語(yǔ)言相對(duì)簡(jiǎn)單多了,直接擼
import UIKit
/// label 的對(duì)齊類型
public enum VerticalAlignment {
case top
case middle
case bottom
}
class MYLabel: UILabel {
var verticalAlignment : VerticalAlignment?
override init(frame: CGRect) {
super.init(frame: frame)
self.verticalAlignment = VerticalAlignment.middle
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
var textRect: CGRect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
switch self.verticalAlignment {
case .top?:
textRect.origin.y = bounds.origin.y
case .bottom?:
textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height
case .middle?:
fallthrough
default:
textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0
}
return textRect
}
override func draw(_ rect: CGRect) {
let rect : CGRect = self.textRect(forBounds: rect, limitedToNumberOfLines: self.numberOfLines)
super.drawText(in: rect)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
其效果圖如上圖:就不再上傳了
Demo位置github位置
最后: 自己創(chuàng)建了一個(gè) iOS 開(kāi)發(fā)群185377619
,有需要的小伙伴加一下,大家共同進(jìn)步