UITextKit框架的学习(一)
来源:互联网 发布:mastercam自动编程 编辑:程序博客网 时间:2024/05/29 08:51
利用NSTextAttachment实现简单的图文混排
//添加UItextView,NSTextView是支持富文本编辑器的控件 UITextView *label=[[UITextView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40, 300)]; [self.view addSubview:label]; //设置内容 NSMutableAttributedString *string =[[NSMutableAttributedString alloc]initWithString:@"123456789101112" attributes:nil]; NSTextAttachment *textAttachment = [[ NSTextAttachment alloc ] initWithData:nil ofType:nil ] ; UIImage * smileImage = [UIImage imageNamed:@"test.jpg"];// 方法1 textAttachment.image = [self image:smileImage byScalingToSize:CGSizeMake(20,20)];// 方法2// textAttachment.image =smileImage;// textAttachment.bounds=CGRectMake(0, 0, 20, 20); NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ; [ string insertAttributedString:textAttachmentString atIndex:6 ] ; label.attributedText = string ;
#pragma makr-返回修改大小后的图片- (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize { UIImage *sourceImage = image; UIImage *newImage = nil; UIGraphicsBeginImageContext(targetSize); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = CGPointZero; thumbnailRect.size.width = targetSize.width; thumbnailRect.size.height = targetSize.height; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage ;}
AttributedString属性参照
// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
// NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象
添加字体,颜色
//添加UItextView,NSTextView是支持富文本编辑器的控件 UITextView *label=[[UITextView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40, 300)]; [self.view addSubview:label]; //设置内容 NSString *originStr = @"Hello,中秋节!"; NSMutableAttributedString *string =[[NSMutableAttributedString alloc]initWithString:originStr attributes:nil]; //给所有字符设置字体为Zapfino,字体高度为15像素 [string addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 15] range: NSMakeRange(0, originStr.length)]; //分段控制,最开始5个字符颜色设置成蓝色 [string addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 5)]; //分段控制,第6个字符开始的3个字符,设置为红色 [string addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(6, 3)]; label.attributedText = string ;
其他设置
//添加UItextView,NSTextView是支持富文本编辑器的控件
UITextView *label=[[UITextView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40, 300)];
[self.view addSubview:label];
//设置内容NSString *originStr = @"Hello,过大年!";//设置背景色NSDictionary *attrDict1 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };//设置字体颜色 NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor redColor] };//设置字体连体//NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符,// 2 表示使用所有连体符号,默认值为 1(iOS 不支持 2)NSDictionary *attrDict3 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0], NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] };//设置字体间距 //NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄NSDictionary *attrDict4 = @{ NSKernAttributeName: @(0), NSFontAttributeName: [UIFont systemFontOfSize: 20] };//设置删除线//NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值// NSUnderlineStyleNone 不设置删除线// NSUnderlineStyleSingle 设置删除线为细单实线// NSUnderlineStyleThick 设置删除线为粗单实线// NSUnderlineStyleDouble 设置删除线为细双实线NSDictionary *attrDict5 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick), NSFontAttributeName: [UIFont systemFontOfSize:20] };//设置双删除线NSDictionary *attrDict6 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble), NSFontAttributeName: [UIFont systemFontOfSize:20] };//设置删除线及颜色 //NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色NSDictionary *attrDict7 = @{ NSStrikethroughColorAttributeName: [UIColor orangeColor], NSStrikethroughStyleAttributeName: @(3), NSFontAttributeName: [UIFont systemFontOfSize:20] };//设置下划线及颜色 //NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色NSDictionary *attrDict8 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor], NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick), NSFontAttributeName: [UIFont systemFontOfSize:20] };//NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果NSDictionary *attrDict9 = @{ NSStrokeWidthAttributeName: @(-3), NSFontAttributeName: [UIFont systemFontOfSize:30] };//设置斜影NSShadow *shadow = [[NSShadow alloc] init]; //NSShadow 对象比较简单,只有3个属性:阴影颜色,模糊半径和偏移shadow.shadowOffset = CGSizeMake(3, 3); //阴影偏移(X方向偏移和Y方向偏移)shadow.shadowBlurRadius = 0.5; //模糊半径shadow.shadowColor = [UIColor orangeColor]; //阴影颜色NSDictionary *attrDict10 = @{ NSShadowAttributeName: shadow, NSFontAttributeName: [UIFont systemFontOfSize:20] };//设置文字特效//NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有一个可用的特效://NSTextEffectLetterpressStyle(凸版印刷效果),适用于iOS 7.0及以上NSDictionary *attrDict11 = @{ NSTextEffectAttributeName: NSTextEffectLetterpressStyle, NSForegroundColorAttributeName: [UIColor grayColor], NSFontAttributeName: [UIFont systemFontOfSize:30] };//设置文字基线//NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏NSDictionary *attrDict12 = @{ NSBaselineOffsetAttributeName: @(-10), NSFontAttributeName: [UIFont systemFontOfSize:20] };//设置文字斜体方向//NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾NSDictionary *attrDict13 = @{ NSObliquenessAttributeName: @(-0.5), NSFontAttributeName: [UIFont systemFontOfSize:30] };//设置文字拉伸//NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本NSDictionary *attrDict14 = @{ NSExpansionAttributeName: @(-1), NSFontAttributeName: [UIFont systemFontOfSize:20] };//设置文字书写方向//NSWritingDirectionAttributeName 设置文字书写方向,取值为以下组合//@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]//@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]//@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]//@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]NSDictionary *attrDict15 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)], NSFontAttributeName: [UIFont systemFontOfSize:20] };//设置打开链接//NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址NSDictionary *attrDict16 = @{ NSLinkAttributeName: [NSURL URLWithString: @"http://www.baidu.com"],NSFontAttributeName: [UIFont systemFontOfSize:20] };label.editable = NO; //必须禁止输入,否则点击将弹出输入键盘label.scrollEnabled = NO; //可选label.delegate = self; //必须设置,否则代理函数不会被回调//设置文字对齐方式// alignment 对齐方式,取值枚举常量 NSTextAlignment// enum {// NSTextAlignmentLeft = 0,// NSTextAlignmentCenter = 1,// NSTextAlignmentRight = 2,// NSTextAlignmentJustified = 3,// NSTextAlignmentNatural = 4,// };// typedef NSInteger NSTextAlignment;label.text = @"alignment : NSTextAlignmentCenter";label.text = @"alignment : NSTextAlignmentJustified";// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.alignment = NSTextAlignmentNatural;NSDictionary *attrDict17 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] };//设置首行缩进//firstLineHeadIndent 首行缩进,取值 floatlabel.text = @"firstLineHeadIndent: 24";label.text = @"firstLineHeadIndent: 48";// NSParagraphStyleNSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.firstLineHeadIndent = 24;NSDictionary *attrDict18 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] };//除首行的缩进label.text = @"headIndent: 24";label.text = @"headIndent: 48";// NSParagraphStyleNSMutableParagraphStyle *paraStyle03 = [[NSMutableParagraphStyle alloc] init];paraStyle03.headIndent = 24;NSDictionary *attrDict19 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] };//行尾缩进label.text = @"tailIndent: 48";label.text = @"tailIndent: 252";// NSParagraphStyleNSMutableParagraphStyle *paraStyle04 = [[NSMutableParagraphStyle alloc] init];paraStyle04.tailIndent = 48;NSDictionary *attrDict20 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] };//设置行高label.text = @"lineHeightMultiple: 0.6";label.text = @"lineHeightMultiple: 2.5";// NSParagraphStyleNSMutableParagraphStyle *paraStyle05 = [[NSMutableParagraphStyle alloc] init];paraStyle05.lineHeightMultiple = 0.6;NSDictionary *attrDict21 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] };//设置段落label.text = @"paragraphSpacingBefore: -7";label.text = @"paragraphSpacingBefore: 25";// NSParagraphStyleNSMutableParagraphStyle *paraStyle06 = [[NSMutableParagraphStyle alloc] init];paraStyle06.paragraphSpacingBefore = -7;NSDictionary *attrDict22 = @{ NSParagraphStyleAttributeName: paraStyle06, NSFontAttributeName: [UIFont systemFontOfSize: 12] };//行文本切断//lineBreakMode 断行方式,取值枚举常量 NSLineBreakMode// enum {// NSLineBreakByWordWrapping = 0, //自动换行,单词切断// NSLineBreakByCharWrapping, //自动换行,字母切断// NSLineBreakByClipping, //非自动换行,不切断// NSLineBreakByTruncatingHead, //非自动换行,行首切断// NSLineBreakByTruncatingTail, //非自动换行,行尾切断// NSLineBreakByTruncatingMiddle //非自动换行,中间切断// };// typedef NSUInteger NSLineBreakMode;label.text = @"lineBreakMode: NSLineBreakByTruncatingHead";label.text = @"lineBreakMode: NSLineBreakByTruncatingTail";// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.lineBreakMode = NSLineBreakByTruncatingHead;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] };//开启断词功能_label11.text = @"hyphenationFactor: 0.3";_label12.text = @"hyphenationFactor: 0.9";// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.hyphenationFactor = 0.3;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 15] };label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict16];
- UITextKit框架的学习(一)
- UITextKit框架的学习(二)
- 框架的学习(一)--形而上学篇
- SpringMVC学习(一)--框架的搭建
- Spring框架的学习(一)
- XUtils框架的学习(一)
- xUtils框架的学习(一)
- 关于Struts2框架的学习(一)
- MQ框架的学习(一)
- JavaScript框架学习(一)
- yii框架学习(一)
- 次世代框架学习(一)
- spring框架学习(一)
- Netty框架学习(一)
- MINA框架学习 (一)
- Spring框架学习(一)
- Spring框架学习(一)
- spring框架学习(一)
- Swift注释风格
- android中sharedPreferences的用法与json和数组的互转
- 浅谈依赖注入
- 压缩跟踪Compressive Tracking
- JAVA随堂笔记课【三】:面向对象
- UITextKit框架的学习(一)
- hdoj--1342--Lotto(dfs)
- 零基础入门学习Python(8):字符串格式化
- 抽象工厂(Abstact factory)模式
- 压缩跟踪Compressive Tracking源码理解
- 简单易用的高速加密工具 BCArchive
- 基于rails的schedule网站开发(7):部署到heroku
- Error when loading the SDK:解决方案
- 使用TortoiseGit上传代码到Github上