UILabel的使用

1:UILabel显示不同字体大小、颜色




2:动态计算文字大小

/**
 *
 *   @param font 字体大小
 *   @param size 限制的size
 */
-(CGSize)newSizeWithFont:(UIFont *)font andSize:(CGSize)size{
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading  attributes:tdic context:nil].size;
}

 例如:  CGSize lableSize=[lable.text newSizeWithFont:[UIFont systemFontOfSize:13] andSize:CGSizeMake(320, 60)];



3:label文字显示位置,靠顶部,居中,靠底部


实现代码如下:

在.h文件里:

#import <UIKit/UIKit.h>

ypedef enum
{
    VerticalAlignmentTop = 0, // default
    VerticalAlignmentMiddle,
    VerticalAlignmentBottom,
} VerticalAlignment;
@interface Mylabel : UILabel{//自定义UILabel
@private
    VerticalAlignment _verticalAlignment;
}

@property (nonatomic) VerticalAlignment verticalAlignment;

@end


在.m文件里:

#import "Mylabel.h"

@implementation Mylabel
@synthesize verticalAlignment = verticalAlignment_;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.verticalAlignment = VerticalAlignmentMiddle;
    }
    return self;
}

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    verticalAlignment_ = verticalAlignment;
    [self setNeedsDisplay];
}

- (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:
        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


4:UILabel上加删除线

首先生成一个继承与UILabel的类SFPStrikeThroughAndUnderLineLabel

一,在.h文件里

@interface SFPStrikeThroughAndUnderLineLabel : UILabel

{

    BOOL _isWithStrikeThrough;   

}

 @property (nonatomicassignBOOL isWithStrikeThrough; //控制是否显示删除线

@property (nonatomicassignCGFloat strikeThroughLineWidth;//设置删除线的宽度

@property (nonatomicretainNSArray *strikeThroughRGBAlphaArray;//设置删除线的颜色,数组里面的元素分别是RGB以及alpha值也就是说该数组有四个元素

@end

二,在.m文件里


#import "SFPStrikeThroughAndUnderLineLabel.h"

@implementation SFPStrikeThroughAndUnderLineLabel

@synthesize isWithStrikeThrough = _isWithStrikeThrough;

@synthesize strikeThroughRGBAlphaArray = _strikeThroughRGBAlphaArray;

@synthesize strikeThroughLineWidth = _strikeThroughLineWidth;


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.strikeThroughRGBAlphaArray = [[NSArrayalloc]initWithObjects:@"0",@"0",@"0",@"1"nil];//默认删除线的颜色是黑色切实不透明的,给个默认值,建立对象时该属性可以不必赋值
        self.strikeThroughLineWidth = 1;//默认删除线的宽度是1pix
    }

    returnself;

}

 

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    if (self.isWithStrikeThrough)//显示删除线

    {

        CGContextRef c = UIGraphicsGetCurrentContext();

        CGFloat color[4] = {[[self.strikeThroughRGBAlphaArrayobjectAtIndex:0floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:1floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:2floatValue], [[self.strikeThroughRGBAlphaArrayobjectAtIndex:3floatValue]};

        CGContextSetStrokeColor(c, color);

        CGContextSetLineWidth(c, self.strikeThroughLineWidth);

        CGContextBeginPath(c);

        CGFloat halfWayUp = (self.bounds.size.height - self.bounds.origin.y) / 2.0;

        CGContextMoveToPoint(c, self.bounds.origin.x, halfWayUp );

        CGContextAddLineToPoint(c, self.bounds.origin.x + self.bounds.size.width, halfWayUp);

        CGContextStrokePath(c);

    }

    [super drawRect:rect];

}

三,使用:添加SFPStrikeThroughAndUnderLineLabel.h文件


 SFPStrikeThroughAndUnderLineLabel *oneLabel = [[SFPStrikeThroughAndUnderLineLabelalloc]initWithFrame:CGRectMake(2020100200)];
    oneLabel.backgroundColor = [UIColorwhiteColor];
    oneLabel.numberOfLines = 0;
    oneLabel.text = @"爱你一万年";

    oneLabel.isWithStrikeThrough = YES;

    [self.view addSubview:oneLabel];

注意:这是给UILabel整个控件加删除线(一个label只会有一条删除线),而不是给label的text加删除线 


5:如何在Label中显示图片

#import "ViewController.h"


@interface ViewController ()

//成员变量 storyboard里拖得一个label

@property (weak, nonatomic) IBOutletUILabel *imageLabel;


@end


@implementation ViewController


- (void)viewDidLoad{

    [superviewDidLoad];


    //推荐一种用Label系统的属性来做。

   UIImage *image=[UIImageimageNamed:@"shareIcon_4"];

    

    NSTextAttachment *textAttch=[[NSTextAttachmentalloc]init];

   textAttch.image=image;

    

    NSAttributedString *str=[NSAttributedStringattributedStringWithAttachment:textAttch];

    self.imageLabel.attributedText=str;

    

}


@end



6: 
设置行间距

#import <UIKit/UIKit.h>


@interface UILabel (Utils)


//设置行间距

- (void)setLineSpacing:(CGFloat)space;


@end


#import "UILabel+Utils.h"


@implementation UILabel (Utils)


/**

 *  计算行间距

 *

 *  @param space 位置

 */

- (void)setLineSpacing:(CGFloat)space

{

    if (self.text == nil)

    {

        return;

    }

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.text];

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];

    paragraphStyle.lineSpacing = space;

    NSRange range = NSMakeRange(0, [[NSString alloc]initWithString:self.text].length);

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];

    self.attributedText = attributedString;

}


@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值