http://www.itnose.net/detail/6177538.html
//
// ViewController.m
// JZLIntegrationDemo
//
// Created by 微指 on 15/3/10.
// Copyright (c) 2015年 JZL. All rights reserved.
//
#import "ViewController.h"
#import "JZLLeftTitleRightImgButton.h"
@interface ViewController () <UIScrollViewDelegate>
{
UIScrollView* helpScrView;
UIPageControl* pageCtrl;
BOOL flag;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// self.view.backgroundColor = UIColor.greenColor;
[self richText];
}
- (void)richText
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 500)];
// label.backgroundColor = [UIColor lightGrayColor];
label.numberOfLines = 0;
NSString *str = @"你好,我好,大家好才是真的好!";
// NSString *str =
// @"n"
// @"Here is some simple text that isdfasdfncludejljls bold and italics.n"
// @"We can even include some color.";
// 创建可变属性化字符串
NSMutableAttributedString *attrString =
[[NSMutableAttributedString alloc] initWithString:str];
NSUInteger length = [str length];
//01. 设置基本颜色
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor]
range:NSMakeRange(0, 10)];
//02. 设置基本字体
UIFont *baseFont =[UIFont fontWithName: @"Zapfino" size: 18] ;
[attrString addAttribute:NSFontAttributeName value:baseFont
range:NSMakeRange(0, length)];
//03. 连体属性
[attrString addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:1]
range:NSMakeRange(0, length)];
//04. 字符间距 -- 正值间距加宽,负值间距变窄
[attrString addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:5]
range:NSMakeRange(0, length)];
//05. 删除线
[attrString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:1]
range:[str rangeOfString:@"大家好"]];
//06. 删除线颜色
[attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor lightGrayColor]
range:[str rangeOfString:@"大家好"]];
//07. 下划线
[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1]
range:[str rangeOfString:@"大家好"]];
//08. 下划线颜色
[attrString addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor]
range:[str rangeOfString:@"大家好"]];
//09. 笔画宽度 - 负值填充效果,正值中空效果
[attrString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:-5]
range:[str rangeOfString:@"大家好"]];
//10. 填充部分颜色,不是字体颜色
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor cyanColor]
range:[str rangeOfString:@"大家好"]];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor yellowColor];
shadow.shadowBlurRadius = 0.5;
shadow.shadowOffset = CGSizeMake(36.0, 16.0);
//11. 阴影属性
[attrString addAttribute:NSShadowAttributeName value:shadow
range:[str rangeOfString:@"大家好"]];
//12. 文本特殊效果 -- NSTextEffectLetterpressStyle(凸版印刷效果),适用于iOS 7.0及以上
[attrString addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle
range:[str rangeOfString:@"我好"]];
//13. 基线偏移值 ---- 取值为 NSNumber (float),正值上偏,负值下偏
[attrString addAttribute:NSBaselineOffsetAttributeName
value:[NSNumber numberWithFloat:10]
range:[str rangeOfString:@"你好,"]];
//14. 字形倾斜度 ---- 取值为 NSNumber (float),正值右倾,负值左倾
[attrString addAttribute:NSObliquenessAttributeName
value:[NSNumber numberWithFloat:1.0]
range:[str rangeOfString:@"你好,"]];
//15. 横向拉伸属性 ---- 取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
[attrString addAttribute:NSExpansionAttributeName
value:[NSNumber numberWithFloat:1.0]
range:[str rangeOfString:@"我"]];
//16. 设置文字书写方向,取值为以下组合
[attrString addAttribute:NSWritingDirectionAttributeName
value:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]
range:[str rangeOfString:@"我好"]];
//17. 设置文字排版防线,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本。
// 在 iOS 中,总是使用横排文本,0 以外的值都未定义
[attrString addAttribute:NSVerticalGlyphFormAttributeName
value:@(1)
range:[str rangeOfString:@"我好"]];
//18. 设置链接属性,点击后调用浏览器打开指定URL地址
[attrString addAttribute:NSLinkAttributeName
value:[NSURL URLWithString: @"http://www.baidu.com"]
range:[str rangeOfString:@"我好"]];
// _textview01.editable = NO; //必须禁止输入,否则点击将弹出输入键盘
// _textview01.scrollEnabled = NO; //可选
// _textview01.delegate = self; //必须设置,否则代理函数不会被回调
//19. 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
[attrString addAttribute:NSLinkAttributeName
value:[NSURL URLWithString: @"http://www.baidu.com"]
range:[str rangeOfString:@"我好"]];
NSTextAttachment *textAttachment01 = [[NSTextAttachment alloc] init];
textAttachment01.image = [UIImage imageNamed:@"1.png"]; // 图片源
textAttachment01.bounds = CGRectMake(0, 0, 30, 30); // 设置图片的位置和大小
NSAttributedString *attributeStr = [NSAttributedString attributedStringWithAttachment:textAttachment01];
[attrString insertAttributedString:attributeStr atIndex:3];
// 20. 设置文本段落排版格式,取值为 NSParagraphStyle/NSMutableParagraphStyle 对象
// alignment 对齐方式,取值枚举常量 NSTextAlignment
// firstLineHeadIndent 首行缩进,取值 float
// headIndent 非首行缩进,取值 float
// tailIndent 尾部缩进,取值 float (行尾缩进,注意距离是从行首算起--距离行首的长度)
// lineHeightMultiple 可变行高,乘因数,取值 float (大于1行高变小,小于1行高变小,实际上字体大小不会改变,改变的时行间距)
// maximumLineHeight 最大行高,取值 float (若其值小于默认行高,则行间距变小,若其值大于默认行高,则不会引起任何变化)
// minimumLineHeight 最小行高,取值 float (若其值大于默认行高,则行间距变大,若其值小于默认行高,则不会引起任何变化)
// lineSpacing 行距,取值 float (行距,取值为 float,可正可负,正值增加行距,负值减小行距)
// paragraphSpacing 段距,取值 float (段距,取值 float, 负值无效,取0值)
// paragraphSpacingBefore 段首空间,取值 float (段首距离,取值 float , 最小取值为0)
//
// baseWritingDirection 句子方向,取值枚举常量 NSWritingDirection (句子排版方向,取值为枚举常量 NSWritingDirection)
// lineBreakMode 断行方式,取值枚举常量 NSLineBreakMode (断行方式,取值枚举常量 NSLineBreakMode)
// hyphenationFactor 连字符属性,取值 0 - 1 (连字符属性,取值 0 到 1 之间,开启断词功能)
// NSLineBreakByWordWrapping = 0, //自动换行,单词切断
// NSLineBreakByCharWrapping, //自动换行,字母切断
// NSLineBreakByClipping, //非自动换行,不切断 (非自动换行,每段一行)
// NSLineBreakByTruncatingHead, //非自动换行,行首切断
// NSLineBreakByTruncatingTail, //非自动换行,行尾切断
// NSLineBreakByTruncatingMiddle //非自动换行,中间切断
label.attributedText = attrString;
[self.view addSubview:label];
// -----------------------------UITextView--------------------------------------
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 250, 40)];
textView.editable = NO; //必须禁止输入,否则点击将弹出输入键盘
textView.delegate = self; //必须设置,否则代理函数不会被回调
textView.scrollEnabled = NO; //可选
NSString *baiduUrl = @"www.baidu.com";
NSMutableAttributedString *baidu = [[NSMutableAttributedString alloc] initWithString:baiduUrl];
[baidu addAttribute:NSLinkAttributeName value:[NSURL URLWithString: @"http://www.baidu.com"] range:[baiduUrl rangeOfString:@"www.baidu.com"]];
textView.attributedText = baidu;
textView.font = [UIFont systemFontOfSize:28];
[self.view addSubview:textView];
}
#pragma mark - UITextViewDelegate Methods
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
return YES;
}
#pragma mark - 引导页
- (void)guidePage
{
CGRect bounds = self.view.frame; //获取界面区域
//加载蒙板图片,限于篇幅,这里仅显示一张图片的加载方法
UIImageView* imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height)] ; //创建UIImageView,位置大小与主界面一样。
[imageView1 setImage:[UIImage imageNamed:@"1.png"]]; //加载图片help01.png到imageView1中。
imageView1.alpha = 0.5f; // 将透明度设为50%。
//继续加载图片
//。。。。
//创建UIScrollView
helpScrView = [[UIScrollView alloc] initWithFrame:CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height)]; //创建UIScrollView,位置大小与主界面一样。
[helpScrView setContentSize:CGSizeMake(bounds.size.width * 3, bounds.size.height)]; //设置全部内容的尺寸,这里帮助图片是3张,所以宽度设为界面宽度*3,高度和界面一致。
helpScrView.pagingEnabled = YES; //设为YES时,会按页滑动
helpScrView.bounces = NO; //取消UIScrollView的弹性属性,这个可以按个人喜好来定
[helpScrView setDelegate:self]; //UIScrollView的delegate函数在本类中定义
helpScrView.showsHorizontalScrollIndicator = NO; //因为我们使用UIPageControl表示页面进度,所以取消UIScrollView自己的进度条。
[helpScrView addSubview:imageView1]; //将UIImageView添加到UIScrollView中。
[self.view addSubview:helpScrView]; //将UIScrollView添加到主界面上。
//创建UIPageControl
pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, bounds.size.height - 30, bounds.size.width, 30)]; //创建UIPageControl,位置在屏幕最下方。
pageCtrl.numberOfPages = 3; //总的图片页数
pageCtrl.currentPage = 0; //当前页
[pageCtrl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; //用户点击UIPageControl的响应函数
[self.view addSubview:pageCtrl]; //将UIPageControl添加到主界面上。
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//更新UIPageControl的当前页
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.frame;
[pageCtrl setCurrentPage:offset.x / bounds.size.width];
}
- (void)pageTurn:(UIPageControl*)sender
{
//令UIScrollView做出相应的滑动显示
CGSize viewSize = helpScrView.frame.size;
CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
[helpScrView scrollRectToVisible:rect animated:YES];
}
#pragma mark - viewMask
- (void)viewMask
{
UIView *bg = [[UIView alloc] initWithFrame:self.view.frame];
bg.backgroundColor = UIColor.blueColor;
// Create a mask layer and the frame to determine what will be visible in the view.
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = CGRectMake(50, 50, 20, 20);
// Create a path and add the rectangle in it.
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, maskRect);
// Set the path to the mask layer.
[maskLayer setPath:path];
// Release the path since it's not covered by ARC.
CGPathRelease(path);
// Set the mask of the view.
bg.layer.mask = maskLayer;
[self.view addSubview:bg];
}
#pragma mark - naviBarCenterButton
- (void)naviBarCenterButton
{
JZLLeftTitleRightImgButton *jzlbutton = [[JZLLeftTitleRightImgButton alloc] initWithFrame:CGRectMake(20, 40, 120, 40)];
// [jzlbutton setBackgroundColor:[UIColor greenColor]];
[jzlbutton setTitle:@"我是新按钮" forState:UIControlStateNormal];
[jzlbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[jzlbutton setImage:[UIImage imageNamed:@"chengshi"] forState:UIControlStateNormal];
[jzlbutton addTarget:self action:@selector(xuanzhuan:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:jzlbutton];
flag = YES;
}
- (void)xuanzhuan:(JZLLeftTitleRightImgButton *)sender
{
#warning 抖动
[UIView animateWithDuration:0.2 animations:^{
CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
shake.fromValue = [NSNumber numberWithFloat:-0.5];
shake.toValue = [NSNumber numberWithFloat:+0.5];
//是否自动返回
shake.autoreverses = YES;
//持续时间
shake.duration = 0.2;
//重复次数
shake.repeatCount = 3;
[sender.imageView.layer addAnimation:shake forKey:@"imageShake"];
}];
#warning 旋转
if (flag) {
[UIView animateWithDuration:0.5 animations:^{
sender.imageView.transform = CGAffineTransformMakeRotation(M_PI);
} completion:^(BOOL finished) {
flag = NO;
}];
}
else {
[UIView animateWithDuration:0.5 animations:^{
sender.imageView.transform = CGAffineTransformMakeRotation(0);
} completion:^(BOOL finished) {
flag = YES;
}];
}
}
//rotationGR.view.transform = CGAffineTransformRotate(rotationGR.view.transform, rotationGR.rotation);
//rotationGR.rotation = 0;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end