findViewsWithText的使用

有利的起头化view以及安装tag值,伊始化viewtag

造福的初叶化view以及安装tag值

图片 1

 

效果

图片 2

 

源码

 中的 SetRect

//
//  AccessViewTagProtocol.h
//  Animations
//
//  Created by YouXianMing on 16/6/17.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^ViewSetupBlock)(UIView * view);

@protocol AccessViewTagProtocol <NSObject>

@required

/**
 *  Set the view whose tag matches the specified value.
 *
 *  @param view View.
 *  @param tag  tag.
 */
- (void)setView:(UIView *)view withTag:(NSInteger)tag;

/**
 *  Remove the tag.
 *
 *  @param tag View's tag.
 */
- (void)removeReferenceWithTag:(NSInteger)tag;

/**
 *  Get the view from the tag.
 *
 *  @param tag.
 *
 *  @return view's object.
 */
- (id)viewWithTag:(NSInteger)tag;

@end

//
//  UIView+FrameAndTag.h
//  SetRect
//
//  Created by YouXianMing on 16/6/19.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AccessViewTagProtocol.h"

@interface UIView (FrameAndTag) <AccessViewTagProtocol>

#pragma mark - Set tags.

/**
 *  Support AccessViewTagProtocol.
 */
- (void)supportAccessViewTagProtocol;

/**
 *  Get the view with specified tag from CustomViewController type's controller.
 *
 *  @param tag    View's tag.
 *  @param object AccessViewTagProtocol's object.
 *
 *  @return The view.
 */
+ (instancetype)viewWithTag:(NSInteger)tag from:(id <AccessViewTagProtocol>)object;

/**
 *  Set the view's tag.
 *
 *  @param tag    View's tag.
 *  @param object AccessViewTagProtocol's object.
 */
- (void)setTag:(NSInteger)tag attachedTo:(id <AccessViewTagProtocol>)object;

#pragma mark - Init frames.

/**
 *  设置尺寸以及设置tag值
 */
+ (instancetype)viewWithFrame:(CGRect)frame insertIntoView:(UIView *)view tag:(NSInteger)tag
                   attachedTo:(id <AccessViewTagProtocol>)object setupBlock:(ViewSetupBlock)block;

/**
 *  设置尺寸
 */
+ (instancetype)viewWithFrame:(CGRect)frame insertIntoView:(UIView *)view setupBlock:(ViewSetupBlock)block;

#pragma mark - Init line view.

/**
 *  水平线条
 */
+ (instancetype)lineViewInsertIntoView:(UIView *)view positionY:(CGFloat)positionY thick:(CGFloat)thick
                               leftGap:(CGFloat)leftGap rightGap:(CGFloat)rightGap color:(UIColor *)color;

/**
 *  垂直线条
 */
+ (instancetype)lineViewInsertIntoView:(UIView *)view positionX:(CGFloat)positionX thick:(CGFloat)thick
                                topGap:(CGFloat)topGap bottomGap:(CGFloat)bottomGap color:(UIColor *)color;

@end

NS_INLINE id viewFrom(id <AccessViewTagProtocol> object, NSInteger tag) {

    return [UIView viewWithTag:tag from:object];
}

//
//  UIView+FrameAndTag.m
//  SetRect
//
//  Created by YouXianMing on 16/6/19.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import <objc/runtime.h>
#import "UIView+FrameAndTag.h"

@interface UIView ()

@property (nonatomic, strong) NSNumber  *tagNumberValue;
@property (nonatomic, strong) NSMapTable <NSString *, UIView *> *viewsWeakMap;

@end

@implementation UIView (FrameAndTag)

- (void)supportAccessViewTagProtocol {

    self.viewsWeakMap = [NSMapTable strongToWeakObjectsMapTable];
}

+ (instancetype)viewWithTag:(NSInteger)tag from:(id <AccessViewTagProtocol>)object {

    return [object viewWithTag:tag];
}

- (void)setTag:(NSInteger)tag attachedTo:(id <AccessViewTagProtocol>)object {

    self.tagNumberValue ? [object removeReferenceWithTag:self.tagNumberValue.integerValue] : 0;
    self.tag            = tag;
    self.tagNumberValue = @(tag);
    [object setView:self withTag:tag];
}

+ (instancetype)viewWithFrame:(CGRect)frame
               insertIntoView:(UIView *)view
                          tag:(NSInteger)tag
                   attachedTo:(id <AccessViewTagProtocol>)object
                   setupBlock:(ViewSetupBlock)block {

    UIView *tmpView = [[[self class] alloc] initWithFrame:frame];
    [tmpView supportAccessViewTagProtocol];

    view   && [view isKindOfClass:[UIView class]]                     ? ([view addSubview:tmpView])              : 0;
    object && [object respondsToSelector:@selector(setView:withTag:)] ? ([tmpView setTag:tag attachedTo:object]) : 0;

    if (block) {

        block(tmpView);
    }

    return tmpView;
}

+ (instancetype)viewWithFrame:(CGRect)frame
               insertIntoView:(UIView *)view
                   setupBlock:(ViewSetupBlock)block {

    UIView *tmpView = [[[self class] alloc] initWithFrame:frame];
    [tmpView supportAccessViewTagProtocol];

    view && [view isKindOfClass:[UIView class]] ? ([view addSubview:tmpView]) : 0;

    if (block) {

        block(tmpView);
    }

    return tmpView;
}

+ (instancetype)lineViewInsertIntoView:(UIView *)view positionY:(CGFloat)positionY thick:(CGFloat)thick
                               leftGap:(CGFloat)leftGap rightGap:(CGFloat)rightGap color:(UIColor *)color {

    UIView *tmpView = [[[self class] alloc] initWithFrame:CGRectMake(leftGap, positionY, view.frame.size.width - leftGap - rightGap, thick)];
    color ? tmpView.backgroundColor = color : 0;
    [view addSubview:tmpView];

    return tmpView;
}

+ (instancetype)lineViewInsertIntoView:(UIView *)view positionX:(CGFloat)positionX thick:(CGFloat)thick
                                topGap:(CGFloat)topGap bottomGap:(CGFloat)bottomGap color:(UIColor *)color {

    UIView *tmpView = [[[self class] alloc] initWithFrame:CGRectMake(positionX, topGap, thick, view.frame.size.height - topGap - bottomGap)];
    color ? tmpView.backgroundColor = color : 0;
    [view addSubview:tmpView];

    return tmpView;
}

#pragma mark - Runtime property.

- (void)setTagNumberValue:(NSNumber *)tagNumberValue {

    objc_setAssociatedObject(self, @selector(tagNumberValue), tagNumberValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)tagNumberValue {

    return objc_getAssociatedObject(self, _cmd);
}

- (void)setViewsWeakMap:(NSMapTable<NSString *,UIView *> *)viewsWeakMap {

    objc_setAssociatedObject(self, @selector(viewsWeakMap), viewsWeakMap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMapTable<NSString *,UIView *> *)viewsWeakMap {

    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - AccessViewTagProtocol.

- (void)setView:(UIView *)view withTag:(NSInteger)tag {

    [self.viewsWeakMap setObject:view forKey:@(tag).stringValue];
}

- (id)viewWithTag:(NSInteger)tag {

    return [self.viewsWeakMap objectForKey:@(tag).stringValue];
}

- (void)removeReferenceWithTag:(NSInteger)tag {

    [self.viewsWeakMap removeObjectForKey:@(tag).stringValue];
}

@end

 

细节

急需贯彻协议(用NSMapTable的strongToWeakObjectsMapTable来作为存款和储蓄string – view)

图片 3

赢得tag更为有利于,不依据于从哪二个view中获得view,而是向来从NSMapTable中赢得

图片 4

 

便利的早先化view以及安装tag值 效果 源码

便民的初阶化view以及安装tag值

有利的初阶化view以及安装tag值

起因:

项目用境遇二个标题,由于某种语言字体的特征,要针对该语言特地调治行间距

由于涉及到八个页面,每页又有三个多行的textview,难道要对种种textview的初叶化进行一回管理么?怎样优雅的对种种textview进行间距调解?

图片 5

图片 5

思路

是想在rootRiew里找到全部的多行textview,统一调动间距

方案1:

用getChildAt的方法只好get到一层child,如若某个child依然Viewgroup,还索要遍历该ViewGroup,套路太深,还要各样剖断,吃相拾叁分丧权辱国,此路不通。

方案2:

想到前端的css样式的落实格局,android有未有不小可能率给textView加个tag,通过tag来取得某一项目标控件,然后共同设置效率呢?然则却开采findViewwithTag每一遍也不得不回到二个view。继续不通

方案3:

在输入findview的时候,studio提醒了findViewsWithText,开始看到是个void方法,以为不可用,结果长远一看,才明白他就是自个儿想要的!

上代码

 View里的findViewsWithText

图片 7

ViewGroup里重写了该办法

图片 8

一看源码,柳暗花明,递归调用,查找全体符合的view,剖断标准是
contentdiscription是或不是带有寻找值,增加到要出口的ArrayList里

 

 

末段化解办法:

在多行的textView加八个contentDescription
属性,然后传三个空的ArrayList,调用findviewsWithText方法,就能够愉悦的获得全部想要的TextView了。

效果

效果

相关文章