UILable的字体位置设置(上、中、下等)

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;

}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值