GitHub:
https://github.com/Cocoanetics/DTCoreText
接口文档说明:
https://docs.cocoanetics.com/DTCoreText/
DTCoreText库包含三部分Parsing,Layouting,UI。
其中UI包含了我们常使用的类:
DTAttributedLabel
用来代替UILabel使用的控件,显示富文本,继承自 DTAttributedTextContentView,可以使用delegate来处理image和hyperlink。
DTAttributedTextCell
作为tableViewCell来使用
DTAttributedTextContentView
用来显示富文本,不应该直接被使用。
DTAttributedTextView
用来代替UITextView,继承自UIScrollView,里面放置了一个DTAttributedTextContentView用来显示内容
DTLazyImageView
DTWebVideoView
DTLinkButton 每个超链接都转化为一个DTLinkButton来使用
setup:
Linking里的Other Linker Flags
Search Paths里的Header Search Paths
ExtUIView.h
@interface UIView (autoresizing) //带margin的左中右层 - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth margin:(CGRect)marginFrame; //左中右层 - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth; //上中下层 - (void) addSubviewTop:(UIView *)topView middleView:(UIView *)middleView bottomView:(UIView *)bottomView topHeight:(CGFloat)topHeight bottomHeight:(CGFloat)bottomHeight; //左上右下 自适应大小来满足margin - (void) addSubview:(UIView *)aView margin:(CGRect)marginFrame; //自适应margin来满足大小 - (void) addSubview:(UIView *)aView size:(CGSize)aSize; //左上右下 自适应大小来满足margin - (void) setSizeInView:(UIView *)aView margin:(CGRect)marginFrame; //左上右下 自适应margin来满足大小 - (void) setCenterInView:(UIView *)aView size:(CGSize)aSize; - (CGPoint) centerPoint; @end
ExtUIView.m
#import "ExtUIView.h" @implementation UIView (autoresizing) - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth margin:(CGRect)marginFrame { ; } - (void) addSubviewLeft:(UIView *)leftView middleView:(UIView *)middleView rightView:(UIView *)rightView lefWidth:(CGFloat)lefWidth rightWidth:(CGFloat)rightWidth { if(leftView) { leftView.frame = CGRectMake(0, 0, lefWidth, self.frame.size.height); leftView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin; [self addSubview:leftView]; } if(middleView) { middleView.frame = CGRectMake(lefWidth, 0, self.frame.size.width - lefWidth - rightWidth, self.frame.size.height); middleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self addSubview:middleView]; } if(rightView) { rightView.frame = CGRectMake(self.frame.size.width - rightWidth, 0, rightWidth, self.frame.size.height); rightView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin ; [self addSubview:rightView]; } } - (void) addSubviewTop:(UIView *)topView middleView:(UIView *)middleView bottomView:(UIView *)bottomView topHeight:(CGFloat)topHeight bottomHeight:(CGFloat)bottomHeight { if(topView) { topView.frame = CGRectMake(0, 0, self.frame.size.width, topHeight); topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; [self addSubview:topView]; } if(middleView) { middleView.frame = CGRectMake(0, topHeight, self.bounds.size.width, self.frame.size.height - topHeight - bottomHeight); middleView.autoresizingMask =UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth ; [self addSubview:middleView]; } if(bottomView) { bottomView.frame = CGRectMake(0, self.frame.size.height - bottomHeight, self.frame.size.width, bottomHeight); bottomView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin ; [self addSubview:bottomView]; } } //左上右下 - (void) addSubview:(UIView *)aView margin:(CGRect)marginFrame { if (!aView) return; aView.frame = CGRectMake(marginFrame.origin.x, marginFrame.origin.y, self.frame.size.width - marginFrame.origin.x - marginFrame.size.width, self.frame.size.height - marginFrame.origin.y - marginFrame.size.height); aView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self addSubview:aView]; } - (void) addSubview:(UIView *)aView size:(CGSize)aSize { if (!aView) return; aView.center = [self centerPoint]; aView.bounds = CGRectMake(0, 0, aSize.width, aSize.height); aView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; [self addSubview:aView]; } //左上右下 - (void) setSizeInView:(UIView *)aView margin:(CGRect)marginFrame { if (!aView) return; self.frame = CGRectMake(marginFrame.origin.x, marginFrame.origin.y, aView.frame.size.width - marginFrame.origin.x - marginFrame.size.width, aView.frame.size.height - marginFrame.origin.y - marginFrame.size.height); self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; } - (void) setCenterInView:(UIView *)aView size:(CGSize)aSize { if (!aView) return; self.center = [aView centerPoint]; self.bounds = CGRectMake(0, 0, aSize.width, aSize.height); self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleBottomMargin; } - (CGPoint) centerPoint { return CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2); } @end
示例:
UIView *v1 = [[[UIView alloc] init] autorelease]; UIView *v2 = [[[UIView alloc] init] autorelease]; UIView *v3 = [[[UIView alloc] init] autorelease]; UIView *v4 = [[[UIView alloc] init] autorelease]; UIView *v5 = [[[UIView alloc] init] autorelease]; UIView *v6 = [[[UIView alloc] init] autorelease]; UIView *v7 = [[[UIView alloc] init] autorelease]; UIView *v8 = [[[UIView alloc] init] autorelease]; UIView *v9 = [[[UIView alloc] init] autorelease]; UIView *v10 = [[[UIView alloc] init] autorelease]; UIView *v11 = [[[UIView alloc] init] autorelease]; UIView *v12 = [[[UIView alloc] init] autorelease]; v1.backgroundColor=[UIColor cyanColor]; v2.backgroundColor=[UIColor grayColor]; v3.backgroundColor=[UIColor greenColor]; v4.backgroundColor=[UIColor greenColor]; v5.backgroundColor=[UIColor blackColor]; v6.backgroundColor=[UIColor cyanColor]; v7.backgroundColor=[UIColor darkGrayColor]; v8.backgroundColor=[UIColor lightGrayColor]; v9.backgroundColor=[UIColor whiteColor]; v10.backgroundColor=[UIColor blackColor]; v11.backgroundColor=[UIColor orangeColor]; v12.backgroundColor=[UIColor purpleColor]; [self.view addSubviewLeft:v1 middleView:v2 rightView:v3 lefWidth:100.0 rightWidth:100.0]; [v1 addSubviewTop:v7 middleView:v8 bottomView:v9 topHeight:100 bottomHeight:50]; [v8 addSubview:v4 size:CGSizeMake(15, 15)]; [v2 addSubview:v5 size:CGSizeMake(55, 55)]; [v3 addSubview:v6 size:CGSizeMake(15, 15)]; [v9 addSubview:v10 margin:CGRectMake(10, 10, 10, 10)]; [v11 setCenterInView:v2 size:CGSizeMake(50, 50)]; [v12 setSizeInView:v2 margin:CGRectMake(50, 10, 50, 100)]; [v2 addSubview:v11]; [v2 addSubview:v12];
示例图:
项目中需要实现一个gridview 点击某项弹出一个popupwindow 最蛋疼的是一个突出的尖角指定点击的哪一项
然后还需要判断底部空间是否足够显示弹出的popupwindow
这个功能点 我遇到了两个问题
第一个就是尖角的定位了 在一个地方困住了好久
点击某一个item 这时候getTop的值实际是到parentView的距离 这里我漏掉了顶部状态栏的大小 在不同手机上测试 效果始终不理想。
首先是获取状态类高度
Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); return frame.top;
这个方法在oncreate中只会返回0 因为这时候实际上还没有开始页面的绘画 所以我放在了点击监听器中。
如此在监听器中弹出popupwindow就可以比较精确的定位了
第二个算是小问题
怎么在popupwindow弹出的情况下 点击外部区域自动关闭 以为很复杂 结果就是3行代码
popWin = new PopupWindow(popview, dm.widthPixels, 106);//没有设置为focusable popWin.setBackgroundDrawable(new BitmapDrawable());//不明 popWin.setOutsideTouchable(true);