[控件] 多选一的标签

多选一的标签

效果:

特点:

1. 自动根据文本长度计算按钮宽度

2. 良好的设计

缺点(靠你来修改了):

1. 没有解禁更多的参数设置(懒)

2. 动态计算没有做彻底(懒)

3. 没有做动画设置(懒)

源码:

ClassesScrollView.h 与 ClassesScrollView.m

//
//  ClassesScrollView.h
//  Study
//
//  Created by YouXianMing on 15/4/20.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol ClassesScrollViewDelegate <NSObject>
@optional
- (void)classesScrollViewChooseTag:(int)tag title:(NSString *)title;
@end

@interface ClassesScrollView : UIView

@property (nonatomic, weak)   id<ClassesScrollViewDelegate>  delegate;
@property (nonatomic, strong) NSArray                       *classesTitleArray; // 班级标题数组

// 创建出view
- (void)buildViews;

// 设置一开始高亮显示的班级
- (void)firstTag:(int)tag;

@end
//
//  ClassesScrollView.m
//  Study
//
//  Created by YouXianMing on 15/4/20.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ClassesScrollView.h"
#import "ClassButtonView.h"

#define  BASE_TAG 0x1122

@interface ClassesScrollView ()<ClassButtonViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation ClassesScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:self.scrollView];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator   = NO;

        self.scrollView.backgroundColor = [UIColor colorWithRed:0.749 green:0.906 blue:0.969 alpha:1];
    }
    return self;
}

- (void)buildViews {

    UIFont *font = [UIFont systemFontOfSize:14.f];

    CGFloat currentX = 5;

    for (int i = 0; i < self.classesTitleArray.count; i++) {
        NSString *string = self.classesTitleArray[i];

        // 按钮宽度
        CGFloat width = [string widthWithLabelFont:font] + BUTTON_GAP + BUTTON_INER_GAP;

        // 创建出View
        ClassButtonView *buttonView = [[ClassButtonView alloc] initWithFrame:CGRectMake(currentX, 0, 200, 30)];
        buttonView.title            = string;
        buttonView.font             = font;
        [buttonView makeToUse];

        if (i == 0) {
            [buttonView changeToHighlight];
        } else {
            [buttonView changeToNormal];
        }

        buttonView.tag              = i + BASE_TAG;
        buttonView.delegate         = self;

        [self.scrollView addSubview:buttonView];

        currentX += width;
    }

    self.scrollView.contentSize = CGSizeMake(currentX + 5, self.height);
}

- (void)classButtonViewButtonEventWithTitle:(NSString *)title buttonView:(ClassButtonView *)buttonView {

    if (_delegate && [_delegate respondsToSelector:@selector(classesScrollViewChooseTag:title:)]) {
        [_delegate classesScrollViewChooseTag:(int)(buttonView.tag - BASE_TAG) title:title];
    }

    for (int i = 0; i < self.classesTitleArray.count; i++) {
        ClassButtonView *tmpButtonView = (ClassButtonView *)[self.scrollView viewWithTag:i + BASE_TAG];

        if (buttonView.tag == tmpButtonView.tag) {
            [tmpButtonView changeToHighlight];
        } else {
            [tmpButtonView changeToNormal];
        }
    }
}

- (void)firstTag:(int)tag {
    for (int i = 0; i < self.classesTitleArray.count; i++) {
        ClassButtonView *tmpButtonView = (ClassButtonView *)[self.scrollView viewWithTag:i + BASE_TAG];

        if (tmpButtonView.tag == tag + BASE_TAG) {
            [tmpButtonView changeToHighlight];
        } else {
            [tmpButtonView changeToNormal];
        }
    }
}

@end

ClassButtonView.h 与 ClassButtonView.m

//
//  ClassButtonView.h
//  Study
//
//  Created by YouXianMing on 15/4/20.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "UIView+SetRect.h"
#import "NSString+StringHeight.h"

#define BUTTON_INER_GAP  25
#define BUTTON_GAP       10

@class ClassButtonView;

@protocol ClassButtonViewDelegate <NSObject>
@optional
- (void)classButtonViewButtonEventWithTitle:(NSString *)title buttonView:(ClassButtonView *)buttonView;
@end

@interface ClassButtonView : UIView

@property (nonatomic, weak)     id<ClassButtonViewDelegate> delegate;

@property (nonatomic, strong)   NSString   *title;  // 标题
@property (nonatomic, strong)   UIFont     *font;   // 字体
@property (nonatomic, strong)   UIButton   *button; // 按钮

@property (nonatomic, readonly) CGFloat     buttonWidth; // 按钮宽度

// 让参数开始生效(重新计算尺寸)
- (void)makeToUse;

// 改变到普通状态
- (void)changeToNormal;

// 改变到高亮状态
- (void)changeToHighlight;

@end
//
//  ClassButtonView.m
//  Study
//
//  Created by YouXianMing on 15/4/20.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ClassButtonView.h"

@interface ClassButtonView ()

@property (nonatomic) CGFloat buttonWidth;

@end

@implementation ClassButtonView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化按钮
        self.button                    = [[UIButton alloc] initWithFrame:self.bounds];
        self.button.height             = 23.f;
        self.button.layer.cornerRadius = 23.f / 2.f;
        [self addSubview:self.button];

        [self.button addTarget:self
                        action:@selector(buttonEvent)
              forControlEvents:UIControlEventTouchUpInside];
    }

    return self;
}

- (void)buttonEvent {
    if (_delegate && [_delegate respondsToSelector:@selector(classButtonViewButtonEventWithTitle:buttonView:)]) {
        [_delegate classButtonViewButtonEventWithTitle:self.title buttonView:self];
    }
}

- (void)makeToUse {
    // 获取宽度
    _buttonWidth = [self.title widthWithLabelFont:self.button.titleLabel.font];

    self.width = _buttonWidth + BUTTON_INER_GAP + BUTTON_GAP;

    self.button.width  = _buttonWidth + BUTTON_INER_GAP;
    self.button.center = self.middlePoint;
}

- (void)changeToNormal {
    [self.button setTitleColor:[UIColor colorWithRed:0.788 green:0.788 blue:0.788 alpha:1] forState:UIControlStateNormal];
    [self.button setBackgroundColor:[UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:1]];
}

- (void)changeToHighlight {
    [self.button setTitleColor:[UIColor colorWithRed:0.996 green:0.996 blue:0.988 alpha:1] forState:UIControlStateNormal];
    [self.button setBackgroundColor:[UIColor colorWithRed:0.192 green:0.682 blue:0.902 alpha:1]];
}

// 重写setter,getter方法
@synthesize title = _title;
- (void)setTitle:(NSString *)title {
    _title = title;
    [self.button setTitle:title forState:UIControlStateNormal];
}

- (NSString *)title {
    return _title;
}

@synthesize font = _font;
- (void)setFont:(UIFont *)font {
    _font = font;
    self.button.titleLabel.font = font;
}
- (UIFont *)font {
    return _font;
}

@end

工具类:

UIView+SetRect.h 与 UIView+SetRect.m

//
//  UIView+SetRect.h
//  TestPch
//
//  Created by YouXianMing on 14-9-26.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (SetRect)

// Frame
@property (nonatomic) CGPoint viewOrigin;
@property (nonatomic) CGSize  viewSize;

// Frame Origin
@property (nonatomic) CGFloat x;
@property (nonatomic) CGFloat y;

// Frame Size
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;

// Frame Borders
@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat bottom;
@property (nonatomic) CGFloat right;

// Center Point
#if !IS_IOS_DEVICE
@property (nonatomic) CGPoint center;
#endif
@property (nonatomic) CGFloat centerX;
@property (nonatomic) CGFloat centerY;

// Middle Point
@property (nonatomic, readonly) CGPoint middlePoint;
@property (nonatomic, readonly) CGFloat middleX;
@property (nonatomic, readonly) CGFloat middleY;
@property (nonatomic, assign) CGFloat cornerRadius;
@property (nonatomic ,assign) BOOL round;
@end
//
//  UIView+SetRect.m
//  TestPch
//
//  Created by YouXianMing on 14-9-26.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "UIView+SetRect.h"

@implementation UIView (SetRect)

#pragma mark Frame

- (CGPoint)viewOrigin
{
    return self.frame.origin;
}

- (void)setViewOrigin:(CGPoint)newOrigin
{
    CGRect newFrame = self.frame;
    newFrame.origin = newOrigin;
    self.frame = newFrame;
}

- (CGSize)viewSize
{
    return self.frame.size;
}

- (void)setViewSize:(CGSize)newSize
{
    CGRect newFrame = self.frame;
    newFrame.size = newSize;
    self.frame = newFrame;
}

#pragma mark Frame Origin

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setX:(CGFloat)newX
{
    CGRect newFrame = self.frame;
    newFrame.origin.x = newX;
    self.frame = newFrame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setY:(CGFloat)newY
{
    CGRect newFrame = self.frame;
    newFrame.origin.y = newY;
    self.frame = newFrame;
}

#pragma mark Frame Size

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)newHeight
{
    CGRect newFrame = self.frame;
    newFrame.size.height = newHeight;
    self.frame = newFrame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)newWidth
{
    CGRect newFrame = self.frame;
    newFrame.size.width = newWidth;
    self.frame = newFrame;
}

#pragma mark Frame Borders

- (CGFloat)left
{
    return self.x;
}

- (void)setLeft:(CGFloat)left
{
    self.x = left;
}

- (CGFloat)right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right
{
    self.x = right - self.width;
}

- (CGFloat)top
{
    return self.y;
}

- (void)setTop:(CGFloat)top
{
    self.y = top;
}

- (CGFloat)bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom
{
    self.y = bottom - self.height;
}

#pragma mark Center Point

#if !IS_IOS_DEVICE
- (CGPoint)center
{
    return CGPointMake(self.left + self.middleX, self.top + self.middleY);
}

- (void)setCenter:(CGPoint)newCenter
{
    self.left = newCenter.x - self.middleX;
    self.top = newCenter.y - self.middleY;
}
#endif

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterX:(CGFloat)newCenterX
{
    self.center = CGPointMake(newCenterX, self.center.y);
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setCenterY:(CGFloat)newCenterY
{
    self.center = CGPointMake(self.center.x, newCenterY);
}

#pragma mark Middle Point

- (CGPoint)middlePoint
{
    return CGPointMake(self.middleX, self.middleY);
}

- (CGFloat)middleX
{
    return self.width / 2;
}

- (CGFloat)middleY
{
    return self.height / 2;
}

- (void)setCornerRadius:(CGFloat)cornerRadius
{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius  = cornerRadius;
}

- (void)setRound:(BOOL)round
{
    [self setCornerRadius:self.height/2];
}

- (CGFloat)cornerRadius
{
    return  self.layer.cornerRadius ;
}

- (BOOL)round
{
    return NO;
}

@end

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>
#import <UIKit/UIKit.h>

@interface NSString (StringHeight)

/**
 *  计算文本高度
 *
 *  @param font   字体
 *
 *  @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;
}

/**
 *  计算
 *
 *  @param string 文本
 *  @param font   字体
 *
 *  @return 宽度
 */
- (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

使用:

    // 高度设置成30,不要变化
    ClassesScrollView *scrollView = [[ClassesScrollView alloc] initWithFrame:CGRectMake(0, 65, 320, 30)];
    scrollView.classesTitleArray  = @[@"NoZuoNoDie", @"YouXianMing", @"iOS Developer", @"UI Designer"];
    scrollView.delegate           = self;
    [scrollView buildViews];

    [self.view addSubview:scrollView];
时间: 2024-08-10 01:55:26

[控件] 多选一的标签的相关文章

bootstrap-表单控件——复选框checkbox和单选择按钮radio

1.运行效果如图所示 2.实现代码如下 <!DOCTYPE html> <html> <head>     <meta charset="utf-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <title>表单控件--复选框checkbox和单选择按钮radio</title>

ExtJs实现复选框组Checkboxgroup单勾选及复选框组对多个复选框组跨控件全选

ExtJs实现复选框Checkboxgroup单勾选及跨控件全选 由于项目的需要,我们要将EXT的控件Checkboxgroup复选框组改造成但勾选,并且实现一个复选框组控制其他多个复选框组的全选,以下是关于如何实现的代码片段. 1.创建CheckboxGroup对象 1,新建空的EOS6.5的工程,新建com.towngas.tcis. gridlock构件包: 2,在com.towngas.tcis.gridlock构件包的"展现"节点下的"页面资源"节点中创建

wxpython 树形控件全选和取消全选

#encoding:utf-8 import wx import wx.lib.agw.customtreectrl as CT class MyFrame(wx.Frame): def __init__(self, parent): self.checked_items = [] wx.Frame.__init__(self, parent, -1, "customtreectrl") self.custom_tree = CT.CustomTreeCtrl(self, agwSty

C#WinForm datagridview控件一选选一整行

1 UI 2 设置 3 效果1 4 效果2

C#WinForm datagridview控件一选选一整行的效果实现时,取消第一行默认被选取的副效果

1 UI 2 code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 usi

jQueryMobile之控件复选框

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <script src=&quo

tabcontrol 控件的标签的隐藏【C#】

[方法一] 由于最近的项目需要,使用TabControl控件开发一个数据库分布同步向导,在最后封装时发现TabControl控件没有现成的隐藏标签的方法和属性:真是急煞人也!想办法解决吧.互联网的功能就是强大,虽然没有找到现成的解决方法,但我也了解的差不多了.经过尝试可以以如下方式实现TabControl的标签隐藏: 在窗体Load事件中添加: [c-sharp] view plaincopy this.tabControl1.Region = new Region(new RectangleF

Java swing 之 标签控件 和 图标控件的使用

在Swing 中显示文本或提示信息的方法是使用标签,它支持文本字符串和图标.在应用程序的用户界面中,一个简短的文本标签可以使用户知道这些控件的目的,所以标签在Swing中是比较常用的控件. 1.标签的使用 标签由JLabel类定义,父类为JComponent类. 标签可以显示一行只读文本,一个图像或带图像的文本,它并不能产生任何类型的事件,只是简单低显示文本和图片,但是可以使用标签的特性指定标签上文本的对齐方式. JLabel类提供了多种构造方法,这样可以创建多种标签,例如显示只有文本的标签.只

【VC编程技巧】控件?4.4ListBox控件(打造带有复选框的ListBox)

4.4ListBox控件(打造带有复选框的ListBox) 下面通过两种方式构建复选框ListBox 1.对话框中插入ListBox控件(IDC_LIST1,IDC_LIST2),如下设置属性 图1 方式一: 2.对ListBox控件(IDC_LIST1)关联控件变量m_checkListBox1,将控件变量类型CListBox -> CCheckListBox(需要手动完成). //修改控件变量类型:CListBox -> CCheckListBox CCheckListBox m_chec