UILabel+TextAlign.m
1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// UILabel+TextAlign.m
// CNLiveScioLive
//
// Created by CNLive-zxw on 2018/9/21.
// Copyright © 2018年 CNLive. All rights reserved.
//
#import "UILabel+TextAlign.h"
@implementation UILabel (TextAlign)
- (void)setIsTop:(BOOL)isTop {
if (isTop) {
CGSize fontSize = [self.text sizeWithFont:self.font];
// CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName : self.font}];
//控件的高度除以一行文字的高度
int num = self.frame.size.height/fontSize.height;
//计算需要添加换行符个数
int newLinesToPad = num - (int)self.numberOfLines;
self.numberOfLines = 0;
for(int i=0; i<newLinesToPad; i++)
//在文字后面添加换行符"/n"
self.text = [self.text stringByAppendingString:@"\n"];
}
}
- (void)setIsBottom:(BOOL)isBottom {
if (isBottom) {
CGSize fontSize = [self.text sizeWithFont:self.font];
// CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName : self.font}];
//控件的高度除以一行文字的高度
int num = self.frame.size.height/fontSize.height;
//计算需要添加换行符个数
int newLinesToPad = num - (int)self.numberOfLines;
self.numberOfLines = 0;
for(int i=0; i<newLinesToPad; i++)
//在文字前面添加换行符"/n"
self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
}
@end