UITextKit框架的学习(一)
2015-12-12 11:36
531 查看
利用NSTextAttachment实现简单的图文混排
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];
//添加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"; // NSParagraphStyle NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init]; paraStyle01.alignment = NSTextAlignmentNatural; NSDictionary *attrDict17 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] }; //设置首行缩进 //firstLineHeadIndent 首行缩进,取值 float label.text = @"firstLineHeadIndent: 24"; label.text = @"firstLineHeadIndent: 48"; // NSParagraphStyle NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init]; paraStyle02.firstLineHeadIndent = 24; NSDictionary *attrDict18 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] }; //除首行的缩进 label.text = @"headIndent: 24"; label.text = @"headIndent: 48"; // NSParagraphStyle NSMutableParagraphStyle *paraStyle03 = [[NSMutableParagraphStyle alloc] init]; paraStyle03.headIndent = 24; NSDictionary *attrDict19 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] }; //行尾缩进 label.text = @"tailIndent: 48"; label.text = @"tailIndent: 252"; // NSParagraphStyle NSMutableParagraphStyle *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"; // NSParagraphStyle NSMutableParagraphStyle *paraStyle05 = [[NSMutableParagraphStyle alloc] init]; paraStyle05.lineHeightMultiple = 0.6; NSDictionary *attrDict21 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 12] }; //设置段落 label.text = @"paragraphSpacingBefore: -7"; label.text = @"paragraphSpacingBefore: 25"; // NSParagraphStyle NSMutableParagraphStyle *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"; // NSParagraphStyle NSMutableParagraphStyle *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"; // NSParagraphStyle NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init]; paraStyle01.hyphenationFactor = 0.3; NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01, NSFontAttributeName: [UIFont systemFontOfSize: 15] }; label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict16];
相关文章推荐
- UICollectionView 瀑布流 初级
- uitableView 选择跳过后, 跳回 颜色变化 问题
- MIUI 判断悬浮窗权限
- 安装程序找不到office.zh-cn\OfficeMUI.msi。请浏览确定有效的安装源,然后单击”确定“。
- Java并发包中的同步队列SynchronousQueue实现原理
- BlockingQueue解析
- Hbuilder快捷入门
- UI中的继承源图
- hdu 3530 Subsequence 单调队列
- MUI之父页面刷新
- 关于uitableviewcell的accessoryType属性
- Legacy UEFI启动的比较
- BOIS模式 UEFI启动与Legacy启动鉴别
- 如何使用java自动生成数据表id,java.util.UUID类
- UEFI:BIOS下一代
- UI中的自适应高度
- MUI 版本更新
- 288. Unique Word Abbreviation
- ue4内存管理
- ue4中寻路设置navmesh