[控件] 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];

时间: 2024-08-24 12:24:15

[控件] LabelView的相关文章

GitHub开源控件的使用合集

Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 build.gradle.中添加: dependencies { compile 'com.wang.avi:library:2.1.3' } 然后在xml中使用: <com.wang.avi.AVLoadingIndicatorView android:id="@+id/avi" and

Android进阶 — GitHub开源控件的使用合集

1.QuantityView 类似购物车数量调节: 效果图: 项目在GitHub上的地址: https://github.com/himanshu-soni/QuantityView 项目使用: 在gradle中添加 compile 'me.himanshusoni.quantityview:quantity-view:1.1.3' 在XML中添加布局: <?xml version="1.0" encoding="utf-8"?> <LinearL

添加自定义UITableView头部控件方法

1.创建一个继承UITableViewHeaderFooterView的类,拥有一个模型 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加到contentView中) 进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片) - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier { if (se

在DataGridView控件中实现冻结列分界线

我们在使用Office Excel的时候,有很多时候需要冻结行或者列.这时,Excel会在冻结的行列和非冻结的区域之间绘制上一条明显的黑线.如下图: (图1) WinForm下的DataGridView控件也能实现类似的冻结行或者列的功能(参见:http://msdn.microsoft.com/zh-cn/library/28e9w2e1(VS.85).aspx) ,但是呢,DataGridView控件默认不会在冻结列或者行的分界处绘制一个明显的分界线,这样的话,最终用户很难注意到当前有列或者

摆脱Login控件,自己定义登录操作

protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { //在登录过程中,程序自动使用login.aspx进行拦截 //验证用户通过后,自动返回拦截的位置 if (Membership.ValidateUser(this.TextBox1.Text, this.TextBox2.Text)) { this.Session["xh"] = this.TextBox1.Text; FormsAuth

python selenium 处理时间日期控件(十五)

测试过程中经常遇到时间控件,需要我们来选择日期,一般处理时间控件通过层级定位来操作或者通过调用js来实现. 1.首先我们看一下如何通过层级定位来操作时间控件. 通过示例图可以看到,日期控件是无法输入日期,点击后弹出日期列表供我们选择日期,自己找了一个日期控制演示一下,通过两次定位,选择了日期 #-*- coding:utf-8 -*- import time from selenium import webdriver driver = webdriver.Chrome() driver.get

Delphi XE10 dxLayoutControl 控件应用指南

http://www.cnblogs.com/Bonny.Wong/p/7440288.html DevExpress VCL套件是一套非常强大的界面控件,可惜关于Delphi开发方面的说明太少,有些控件使用起来一头雾水,不知从何下手.本节详细介绍在Delphi Xe10 Seattle中如何利用dxLayoutControl 控件来做界面布局. 1.  首先从工具箱面板中将dxLayoutControl放在Form上,设置2个关键属性如下: 属性 属性值 说明 Align alClient 一

Android自己定义控件之轮播图控件

背景 近期要做一个轮播图的效果.网上看了几篇文章.基本上都能找到实现,效果还挺不错,可是在写的时候感觉每次都要单独去又一次在Activity里写一堆代码.于是自己封装了一下.这里仅仅是做了下封装成一个控件,不必每次反复写代码了. 效果图 实现分析 轮播图的功能就是实现左右滑动的广告.图片信息展示,那我们就用ViewPager来实现,由于考虑到用户体验,我们还须要在以下加一个指示器来标示滑动到了第几张轮播图.指示器我们能够用一个线性布局来依据要展示的轮播图设置显示的View,我们要做这种一个控件没

[ ObjectListView ] - ListView的增强控件 - 前言 (翻译)

********************************************************************************** 原  标 题: A Much Easier to Use ListView 原文地址: https://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView 翻       译: 于国栋 http://www.shannon.net.cn *********