自定义UIPageControl、UITextView占位视图

一言不合先上效果图:


自定义UIPageControl、UITextView占位视图_第1张图片
效果预览.gif

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


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

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!

你可能感兴趣的:(自定义UIPageControl、UITextView占位视图)