版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.06.08 |
前言
YYText是一個(gè)專門處理文字的框架,有了它處理文字變得非常方便栏豺,這一篇我繼續(xù)介紹YYText的使用方法撤嫩,希望對(duì)大家能有所幫助。大家如感興趣還可以參考:
1.YYText使用篇(一)
2.YYText使用篇(二)
3.YYText使用篇(三)
4.YYText使用篇(四)
5.YYText使用篇(五)
6.YYText使用篇(六)
7.YYText使用篇(七)
一啤咽、YYText示例
下面我們看另外一個(gè)例子,先看代碼渠脉。
#import "YYTextEditExample.h"
#import "YYText.h"
#import "YYImage.h"
#import "UIImage+YYWebImage.h"
#import "UIView+YYAdd.h"
#import "NSBundle+YYAdd.h"
#import "NSString+YYAdd.h"
#import "UIControl+YYAdd.h"
#import "CALayer+YYAdd.h"
#import "NSData+YYAdd.h"
#import "UIGestureRecognizer+YYAdd.h"
#import "YYTextExampleHelper.h"
@interface YYTextEditExample () <YYTextViewDelegate, YYTextKeyboardObserver>
@property (nonatomic, assign) YYTextView *textView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UISwitch *verticalSwitch;
@property (nonatomic, strong) UISwitch *debugSwitch;
@property (nonatomic, strong) UISwitch *exclusionSwitch;
@end
@implementation YYTextEditExample
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
[self initImageView];
__weak typeof(self) _self = self;
UIView *toolbar;
if ([UIVisualEffectView class]) {
toolbar = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
} else {
toolbar = [UIToolbar new];
}
toolbar.size = CGSizeMake(kScreenWidth, 40);
toolbar.top = kiOS7Later ? 64 : 0;
[self.view addSubview:toolbar];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the season of light, it was the season of darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us. We were all going direct to heaven, we were all going direct the other way.\n\n這是最好的時(shí)代宇整,這是最壞的時(shí)代;這是智慧的時(shí)代芋膘,這是愚蠢的時(shí)代鳞青;這是信仰的時(shí)期霸饲,這是懷疑的時(shí)期;這是光明的季節(jié)臂拓,這是黑暗的季節(jié)厚脉;這是希望之春,這是失望之冬胶惰;人們面前有著各樣事物傻工,人們面前一無(wú)所有;人們正在直登天堂孵滞,人們正在直下地獄中捆。"];
text.yy_font = [UIFont fontWithName:@"Times New Roman" size:20];
text.yy_lineSpacing = 4;
text.yy_firstLineHeadIndent = 20;
YYTextView *textView = [YYTextView new];
textView.attributedText = text;
textView.size = self.view.size;
textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
textView.delegate = self;
if (kiOS7Later) {
textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
} else {
textView.height -= 64;
}
textView.contentInset = UIEdgeInsetsMake(toolbar.bottom, 0, 0, 0);
textView.scrollIndicatorInsets = textView.contentInset;
textView.selectedRange = NSMakeRange(text.length, 0);
[self.view insertSubview:textView belowSubview:toolbar];
self.textView = textView;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[textView becomeFirstResponder];
});
/*------------------------------ Toolbar ---------------------------------*/
//垂直頁(yè)面的開(kāi)關(guān)
UILabel *label;
label = [UILabel new];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.text = @"Vertical:";
label.size = CGSizeMake([label.text widthForFont:label.font] + 2, toolbar.height);
label.left = 10;
[toolbar addSubview:label];
_verticalSwitch = [UISwitch new];
[_verticalSwitch sizeToFit];
_verticalSwitch.centerY = toolbar.height / 2;
_verticalSwitch.left = label.right - 5;
_verticalSwitch.layer.transformScale = 0.8;
[_verticalSwitch addBlockForControlEvents:UIControlEventValueChanged block:^(UISwitch *switcher) {
[_self.textView endEditing:YES];
if (switcher.isOn) {
[_self setExclusionPathEnabled:NO];
_self.exclusionSwitch.on = NO;
}
_self.exclusionSwitch.enabled = !switcher.isOn;
_self.textView.verticalForm = switcher.isOn; /// Set vertical form
}];
[toolbar addSubview:_verticalSwitch];
//調(diào)試頁(yè)面的開(kāi)關(guān)
label = [UILabel new];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.text = @"Debug:";
label.size = CGSizeMake([label.text widthForFont:label.font] + 2, toolbar.height);
label.left = _verticalSwitch.right + 5;
[toolbar addSubview:label];
_debugSwitch = [UISwitch new];
[_debugSwitch sizeToFit];
_debugSwitch.on = [YYTextExampleHelper isDebug];
_debugSwitch.centerY = toolbar.height / 2;
_debugSwitch.left = label.right - 5;
_debugSwitch.layer.transformScale = 0.8;
[_debugSwitch addBlockForControlEvents:UIControlEventValueChanged block:^(UISwitch *switcher) {
[YYTextExampleHelper setDebug:switcher.isOn];
}];
[toolbar addSubview:_debugSwitch];
//Exclusion頁(yè)面開(kāi)關(guān)
label = [UILabel new];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.text = @"Exclusion:";
label.size = CGSizeMake([label.text widthForFont:label.font] + 2, toolbar.height);
label.left = _debugSwitch.right + 5;
[toolbar addSubview:label];
_exclusionSwitch = [UISwitch new];
[_exclusionSwitch sizeToFit];
_exclusionSwitch.centerY = toolbar.height / 2;
_exclusionSwitch.left = label.right - 5;
_exclusionSwitch.layer.transformScale = 0.8;
[_exclusionSwitch addBlockForControlEvents:UIControlEventValueChanged block:^(UISwitch *switcher) {
[_self setExclusionPathEnabled:switcher.isOn];
}];
[toolbar addSubview:_exclusionSwitch];
[[YYTextKeyboardManager defaultManager] addObserver:self];
}
- (void)dealloc
{
[[YYTextKeyboardManager defaultManager] removeObserver:self];
}
#pragma mark - Object Private Function
- (void)setExclusionPathEnabled:(BOOL)enabled
{
if (enabled) {
[self.textView addSubview:self.imageView];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.imageView.frame
cornerRadius:self.imageView.layer.cornerRadius];
self.textView.exclusionPaths = @[path]; /// Set exclusion paths
}
else {
[self.imageView removeFromSuperview];
self.textView.exclusionPaths = nil;
}
}
- (void)initImageView
{
NSData *data = [NSData dataNamed:@"dribbble256_imageio.png"];
UIImage *image = [[YYImage alloc] initWithData:data scale:2];
UIImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
imageView.clipsToBounds = YES;
imageView.userInteractionEnabled = YES;
imageView.layer.cornerRadius = imageView.height / 2;
imageView.center = CGPointMake(kScreenWidth / 2, kScreenWidth / 2);
self.imageView = imageView;
__weak typeof(self) _self = self;
UIPanGestureRecognizer *g = [[UIPanGestureRecognizer alloc] initWithActionBlock:^(UIPanGestureRecognizer *g) {
__strong typeof(_self) self = _self;
if (!self) return;
CGPoint p = [g locationInView:self.textView];
self.imageView.center = p;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.imageView.frame
cornerRadius:self.imageView.layer.cornerRadius];
self.textView.exclusionPaths = @[path];
}];
[imageView addGestureRecognizer:g];
}
#pragma mark - Action && Notification
- (void)edit:(UIBarButtonItem *)item
{
if (_textView.isFirstResponder) {
[_textView resignFirstResponder];
}
else {
[_textView becomeFirstResponder];
}
}
#pragma mark - YYTextViewDelegate
- (void)textViewDidBeginEditing:(YYTextView *)textView
{
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(edit:)];
self.navigationItem.rightBarButtonItem = buttonItem;
}
- (void)textViewDidEndEditing:(YYTextView *)textView
{
self.navigationItem.rightBarButtonItem = nil;
}
#pragma mark - YYTextKeyboardObserver
- (void)keyboardChangedWithTransition:(YYTextKeyboardTransition)transition
{
BOOL clipped = NO;
if (_textView.isVerticalForm && transition.toVisible) {
CGRect rect = [[YYTextKeyboardManager defaultManager] convertRect:transition.toFrame toView:self.view];
if (CGRectGetMaxY(rect) == self.view.height) {
CGRect textFrame = self.view.bounds;
textFrame.size.height -= rect.size.height;
_textView.frame = textFrame;
clipped = YES;
}
}
if (!clipped) {
_textView.frame = self.view.bounds;
}
}
@end
二、實(shí)例效果
下面看一下實(shí)例效果剃斧。
后記
未完轨香,待續(xù)~~~