首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >自定义UIPageControl、UITextView占位视图

自定义UIPageControl、UITextView占位视图

作者头像
且行且珍惜_iOS
发布2018-05-22 16:59:10
发布2018-05-22 16:59:10
1.4K0
举报

一言不合先上效果图:


①、自定义的UIPageControl是继承于UIView,封装好的,可以设置图标大小,形状,图片 ,颜色,间隔,当然,需要的话,也可以自定义图标视图;详情请按快捷键Ctrl + B传送!


②、创建有占位视图的UITextView,主要涉及NSTextStorage、NSLayoutManager、NSTextContainer、UIBezierPath几个类,主要代码如下:

代码语言:javascript
复制
NSString * title = @"我是且行且珍惜_iOS?.如果喜欢的话,点个喜欢呗,点个关注呗,赏根辣条呗!哈萨米达!么么哒!https://github.com/wslcmk";
//段落样式
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//调整行间距
paragraphStyle.maximumLineHeight = 22;
paragraphStyle.minimumLineHeight = 22;
NSDictionary *attributeDictionary = @{NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter" size:12],NSForegroundColorAttributeName :[UIColor whiteColor],NSParagraphStyleAttributeName : paragraphStyle };

//文本存储
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:title];
[textStorage addAttributes:attributeDictionary range:NSMakeRange (0, [title length])];

//布局管理
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];

//文本容器
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(self.view.frame.size.width - 16, 48)];
textContainer.lineFragmentPadding = 0;
textContainer.lineBreakMode = NSLineBreakByTruncatingTail;

//抠取容器UITextView的两个部分
CGRect btnRect = CGRectMake((self.view.frame.size.width - 16)/ 2 - 15,10,40,40);
UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:btnRect];
CGRect imageRect = CGRectMake(0,0,20 + 4,20 + 2);
//贝塞尔
UIBezierPath *newPath1 = [UIBezierPath bezierPathWithRect:imageRect];
textContainer.exclusionPaths = @[newPath,newPath1];
[layoutManager addTextContainer:textContainer];

  UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(8, 200, self.view.frame.size.width - 16, 48) textContainer:textContainer];
[textView sizeToFit];
textView.editable = NO;
textView.backgroundColor = [UIColor orangeColor];
//如果设置为YES,设置的lineBreakMode就没有作用了
textView.scrollEnabled = NO;
textView.textContainerInset = UIEdgeInsetsMake (0,0,0,0);

//自动布局后容器的Frame
CGRect containerRect = [[textView layoutManager] usedRectForTextContainer:textContainer];
textView.frame = CGRectMake(8,200, containerRect.size.width, 48);
[self.view addSubview:textView];  

需要Demo请移驾https://github.com/wslcmk/UIPageControlAndUITextView.git

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016.08.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档