1、看效果
2、介绍用途
例如:下面的设计要求8%的字和返利的框对齐。如图:
碰到这种情况。单纯的uilabel 是无能为力的。有的朋友还不信。可以调整位子来对其啊,但是,你换一个手机他就会又变的。所以,你就跟着我来吧 !本例提供两种方法,其中一种有使用限制。
3、方法一(无限限制的)。
//
// UILabelView.m
// UILabel 的文字对齐Demo
//
// Created by MAC on 16/10/12.
// Copyright © 2016年 NetworkCode小贱. All rights reserved.
//
#import "UILabelView.h"
@implementation UILabelView
-(instancetype)initWithFrame:(CGRect)frame{
if (self==[super initWithFrame:frame]) {
self.backgroundColor = [UIColor redColor ];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
/* 获取上下文*/
CGContextRef contentRef = UIGraphicsGetCurrentContext();
/* 监控对象是否为空*/
NSCAssert(self.text!=nil, @"绘制内容不能为空");
/* 获取文本的宽与高*/
CGFloat SizeHeight = self.bounds.size.height;
CGFloat SizeWeight = self.bounds.size.width;
/* 计算文本的大小*/
CGSize TextSize = [self.text boundingRectWithSize:self.bounds.size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:self.attrs context:nil].size;
CGRect drawRect = CGRectZero;
if (_styple == TextStyleTop || _styple == 0) {
drawRect= CGRectMake((SizeWeight-TextSize.width)*0.5, 0, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleRight){
drawRect= CGRectMake(SizeWeight - TextSize.width, (SizeHeight-TextSize.height)*0.5, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleBottom){
drawRect= CGRectMake((SizeWeight-TextSize.width)*0.5, SizeHeight-TextSize.height, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleLeft){
drawRect= CGRectMake(0, (SizeHeight-TextSize.height)*0.5, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleCenter){
drawRect= CGRectMake((SizeWeight-TextSize.width)*0.5, (SizeHeight-TextSize.height)*0.5, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleLeftTop){
drawRect= CGRectMake(0, 0, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleRightTop){
drawRect= CGRectMake(SizeWeight-TextSize.width, 0, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleLeftBottom){
drawRect= CGRectMake(0, SizeHeight-TextSize.height, TextSize.width, TextSize.height);
}else if(self.styple == TextStyleRightBottom){
drawRect= CGRectMake(SizeWeight-TextSize.width, SizeHeight-TextSize.height, TextSize.width, TextSize.height);
}
/* 进行绘制*/
/*
或者
- (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
*/
[self.text drawInRect:drawRect withAttributes:self.attrs];
}
@end
其使用方法:
#pragma mark -- 测试
-(void)testMakeUI{
UILabelView * firstLabel = [[UILabelView alloc]initWithFrame:CGRectMake(60, 64,200, 30)];
/*
为了能都让你们看到,文字的位置,所以这里设置为红色,你可以通过下面的方式来改变背景:
firstLabel.backgroundColor = [UIColor whiteColor];
*/
/* 注意:如果您没有设置绘制的内容,系统将会抛出一个异常提示:
2016-10-12 16:27:48.172 UILabel 的文字对齐Demo[2617:987072] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '绘制内容不能为空'
*/
firstLabel.text = @"成功QQ吧";
/*如果您不设置文字的显示形式,默认显示为中上部*/
firstLabel.styple = TextStyleCenter;
/* 要改变文字的颜色/字大小*/
NSDictionary * Dict = @{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};
firstLabel.attrs =Dict;
[self.view addSubview:firstLabel];
}
方法是:
#pragma mark -- NSMutableAttributedString 的使用来改变字的位置
-(NSMutableAttributedString*)ChangeTextSeatObject:(UILabel*)objectLabel contentString:(NSString*)string top_Or_Bottom:(BOOL)top_Or_Bottom{
NSMutableAttributedString * AttributedString = [[NSMutableAttributedString alloc]initWithString:string];
CGFloat TextHeightFloat = [string boundingRectWithSize:objectLabel.bounds.size options:NSStringDrawingUsesDeviceMetrics attributes:nil context:nil].size.height;
NSAssert(objectLabel!=nil, @"对象不能为空");
/* 参数的配置*/
float temp = 0.0;
if (top_Or_Bottom) {
temp = 1;
}else{
temp = -1;
}
[AttributedString setAttributes:@{NSBaselineOffsetAttributeName:@(temp *(objectLabel.bounds.size.height - TextHeightFloat)*0.5)} range:NSMakeRange(0, string.length)];
return AttributedString;
}