[控件] LabelView

LabelView

[控件] LabelView

[控件] LabelView

此LabelView是用来将Label显示在固定的View上的,需要计算Label的高度与宽度.

源码:

NSString+StringHeight.h 与 NSString+StringHeight.m

//

//  NSString+StringHeight.h

//  USA

//

//  Created by YouXianMing on 14/12/10.

//  Copyright (c) 2014年 fuhuaqi. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface NSString (StringHeight)



/**

 *  计算文本的高度

 *

 *  @param font  字体

 *  @param width 固定的宽度

 *

 *  @return 高度

 */

- (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width;



/**

 *  计算文本的宽度

 *

 *  @param font 字体

 *

 *  @return 宽度

 */

- (CGFloat)widthWithLabelFont:(UIFont *)font;



@end
//

//  NSString+StringHeight.m

//  USA

//

//  Created by YouXianMing on 14/12/10.

//  Copyright (c) 2014年 fuhuaqi. All rights reserved.

//



#import "NSString+StringHeight.h"



@implementation NSString (StringHeight)



- (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width {

    CGFloat height = 0;

    

    if (self.length == 0) {

        height = 0;

    } else {



        // 字体

        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};

        if (font) {

            attribute = @{NSFontAttributeName: font};

        }

        

        // 尺寸

        CGSize retSize = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)

                                            options:

                          NSStringDrawingTruncatesLastVisibleLine |

                          NSStringDrawingUsesLineFragmentOrigin |

                          NSStringDrawingUsesFontLeading

                                         attributes:attribute

                                            context:nil].size;

        

        height = retSize.height;

    }

    

    return height;

}



- (CGFloat)widthWithLabelFont:(UIFont *)font {

    CGFloat retHeight = 0;

    

    if (self.length == 0) {

        retHeight = 0;

    } else {

        

        // 字体

        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};

        if (font) {

            attribute = @{NSFontAttributeName: font};

        }

        

        // 尺寸

        CGSize retSize = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)

                                            options:

                          NSStringDrawingTruncatesLastVisibleLine |

                          NSStringDrawingUsesLineFragmentOrigin |

                          NSStringDrawingUsesFontLeading

                                         attributes:attribute

                                            context:nil].size;

        

        retHeight = retSize.width;

    }

    

    return retHeight;

}



@end

LabelView.h 与 LabelView.m

//

//  LabelView.h

//  YXMWeather

//

//  Created by XianMingYou on 15/2/16.

//  Copyright (c) 2015年 XianMingYou. All rights reserved.

//



#import <UIKit/UIKit.h>

#import "NSString+StringHeight.h"



@interface LabelView : UIView



/**

 *  文本

 */

@property (nonatomic, strong) NSString  *text;



/**

 *  文本颜色

 */

@property (nonatomic, strong) UIColor   *textColor;



/**

 *  文本字体

 */

@property (nonatomic, strong) UIFont    *font;



/**

 *  背景色

 */

@property (nonatomic, strong) UIColor   *color;



/**

 *  距离顶部的距离

 */

@property (nonatomic) CGFloat gapFromTop;



/**

 *  距离底部的距离

 */

@property (nonatomic) CGFloat gapFromBottom;



/**

 *  距离左侧的距离

 */

@property (nonatomic) CGFloat gapFromLeft;



/**

 *  距离右侧的距离

 */

@property (nonatomic) CGFloat gapFromRight;



/**

 *  创建出view

 */

- (void)buildView;



/**

 *  创建出默认配置的label

 *

 *  @param text   字符串

 *  @param origin 起始位置

 *

 *  @return 实例对象

 */

+ (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin;



@end
//

//  LabelView.m

//  YXMWeather

//

//  Created by XianMingYou on 15/2/16.

//  Copyright (c) 2015年 XianMingYou. All rights reserved.

//



#import "LabelView.h"



@interface LabelView ()



@property (nonatomic) CGFloat          labelWidth;

@property (nonatomic) CGFloat          labelHeight;



@property (nonatomic, strong) UILabel *label;



@end



@implementation LabelView



- (void)buildView {

    // 设置label

    self.label.text      = self.text;

    self.label.font      = self.font;

    self.label.textColor = self.textColor;

    

    // 获取宽度

    self.labelWidth   = [self.text widthWithLabelFont:self.font];

    self.labelHeight  = [self.text heightWithLabelFont:self.font withLabelWidth:MAXFLOAT];

    self.label.width  = self.labelWidth;

    self.label.height = self.labelHeight;



    // 计算间距

    self.label.x = self.gapFromLeft;

    self.label.y = self.gapFromTop;

    

    // 重新设置尺寸

    self.width  = self.labelWidth + self.gapFromLeft + self.gapFromRight;

    self.height = self.labelHeight + self.gapFromTop + self.gapFromBottom;

    

    // 设置背景色

    if (self.color) {

        self.backgroundColor = self.color;

    }

}



@synthesize label = _label;

- (UILabel *)label {

    if (_label == nil) {

        _label = [[UILabel alloc] initWithFrame:CGRectZero];

        _label.textAlignment = NSTextAlignmentCenter;

        [self addSubview:_label];

    }

    

    return _label;

}



+ (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin {

    LabelView *labelView    = [[LabelView alloc] initWithFrame:CGRectMake(origin.x, origin.y, 0, 0)];

    labelView.color         = [UIColor blackColor];

    labelView.text          = text;

    labelView.textColor     = [UIColor whiteColor];

    labelView.font          = [UIFont fontWithName:LATO_BOLD size:8];

    labelView.gapFromLeft   = 10.f;

    labelView.gapFromRight  = 10.f;

    labelView.gapFromTop    = 2.f;

    labelView.gapFromBottom = 2.f;

    

    [labelView buildView];



    return labelView;

}



@end

使用时候的源码:

    LabelView *labelView = [LabelView createWithText:@"YouXianMing" atOrigin:CGPointMake(10, 90)];

    [self.view addSubview:labelView];

 

你可能感兴趣的:(label)