UIlabel - 富文本属性
1.NSKernAttributeName: @10 调整字句 kerning 字句调整
2.NSFontAttributeName : [UIFont systemFontOfSize:_fontSize] 设置字体
3.NSForegroundColorAttributeName :[UIColor redColor] 设置文字颜色
4.NSParagraphStyleAttributeName : paragraph 设置段落样式
5.NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
6.NSBackgroundColorAttributeName: [UIColor blackColor] 设置背景颜色
7.NSStrokeColorAttributeName设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心.
NSStrokeWidthAttributeName这个属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。
同时设置了空心的两个属性,并且NSStrokeWidthAttributeName属性设置为整数,文字前景色就无效果了
效果:
效果:
8. NSStrikethroughStyleAttributeName 添加删除线,strikethrough删除线
效果:
9. NSUnderlineStyleAttributeName 添加下划线
效果:
10. NSShadowAttributeName 设置阴影,单独设置不好使,必须和其他属性搭配才好使。
和这三个任一个都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName
11.NSVerticalGlyphFormAttributeName
该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。
效果:
12. NSObliquenessAttributeName设置字体倾斜。Skew 斜
效果:
13. NSExpansionAttributeName 设置文本扁平化
效果:
Example:
http://shijue.me/show_text/521c396a8ddf876566000007
http://www.tuicool.com/articles/zquENb
http://blog.csdn.net/a451493485/article/details/9454695
http://wiki.eoe.cn/page/iOS_pptl_artile_28190.html
http://www.xue5.com/Mobile/iOS/673562.html
#import "ViewController.h"
#import <CoreText/CoreText.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
//学习内容
/*
1.控件 UIView UILabel UITextField UITextView UIButton
2.字体、大小、单位、颜色
*/
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
label.text = @"Label Text Content, This is a text label things attribute";//默认为空
label.font = [UIFont systemFontOfSize:17];//默认使用系统的17
label.textColor = [UIColor orangeColor];//默认使用文本黑色
label.shadowColor = [UIColor lightGrayColor];//默认没有阴影
label.shadowOffset = CGSizeMake(1,0);//默认是一个向上的阴影(0,-1)
label.textAlignment = NSTextAlignmentCenter;//默认是左对齐
label.lineBreakMode = NSLineBreakByTruncatingTail;//段落样式,默认是最后截断尾巴,用...代替 //富文本的基本数据类型,属性字符串。以此为基础,如果这个设置了相应的属性,则会忽略上面设置的属性,默认为空
NSString *string = label.text;
const CGFloat fontSize = 16.0;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSUInteger length = [string length];
//设置字体
UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//设置所有的字体
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//设置Text这四个字母的字体为粗体
//设置倾斜,需要导入coreText
UIFont *italicFont = GetVariationOfFontWithTrait(baseFont,
kCTFontTraitItalic);
[attrString addAttribute:NSFontAttributeName value:italicFont
range:[string rangeOfString:@"Label"]];
// 设置颜色
UIColor *color = [UIColor redColor];
[attrString addAttribute:NSForegroundColorAttributeName
value:color
range:[string rangeOfString:@"Content"]];
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]]; //可以对这些属性设置值
//字体名称有以下:
// label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24];
[attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[string rangeOfString:@"Label"]];
label.numberOfLines = 2;
NSMutableParagraphStyle *
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineSpacing = 10;//增加行高
style.headIndent = 10;//头部缩进,相当于左padding
style.tailIndent = -10;//相当于右padding
style.lineHeightMultiple = 1.5;//行间距是多少倍
style.alignment = NSTextAlignmentLeft;//对齐方式
style.firstLineHeadIndent = 20;//首行头缩进
style.paragraphSpacing = 10;//段落后面的间距
style.paragraphSpacingBefore = 20;//段落之前的间距
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)]; [attrString addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(0, length)];//字符间距 2pt
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];//设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心
[attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];//空心字,文字边框描述
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[string rangeOfString:@"text"]];//下划线
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];//厚的下划线
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[string rangeOfString:@"things"]];//删除线
[attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"things"]];//删除线蓝色
label.attributedText = attrString; label.highlightedTextColor = [UIColor redColor];//设置文本高亮显示颜色,与highlighted一起使用。
label.highlighted = NO; //高亮状态是否打开
label.enabled = YES;//设置文字内容是否可变
label.userInteractionEnabled = YES;//设置标签是否忽略或移除用户交互。默认为NO
label.baselineAdjustment = UIBaselineAdjustmentNone;//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
// UIBaselineAdjustmentAlignBaselines=0,默认,文本最上端与中线对齐。
// UIBaselineAdjustmentAlignCenters, 文本中线与label中线对齐。
// UIBaselineAdjustmentNone, 文本最低端与label中线对齐。;
[self.view addSubview:label]; /*
字体名如下:
Font Family: American Typewriter
Font: AmericanTypewriter
Font: AmericanTypewriter-Bold Font Family: AppleGothic
Font: AppleGothic Font Family: Arial
Font: ArialMT
Font: Arial-BoldMT
Font: Arial-BoldItalicMT
Font: Arial-ItalicMT Font Family: Arial Rounded MT Bold
Font: ArialRoundedMTBold Font Family: Arial Unicode MS
Font: ArialUnicodeMS Font Family: Courier
Font: Courier
Font: Courier-BoldOblique
Font: Courier-Oblique
Font: Courier-Bold Font Family: Courier New
Font: CourierNewPS-BoldMT
Font: CourierNewPS-ItalicMT
Font: CourierNewPS-BoldItalicMT
Font: CourierNewPSMT Font Family: DB LCD Temp
Font: DBLCDTempBlack Font Family: Georgia
Font: Georgia-Bold
Font: Georgia
Font: Georgia-BoldItalic
Font: Georgia-Italic Font Family: Helvetica
Font: Helvetica-Oblique
Font: Helvetica-BoldOblique
Font: Helvetica
Font: Helvetica-Bold Font Family: Helvetica Neue
Font: HelveticaNeue
Font: HelveticaNeue-Bold Font Family: Hiragino Kaku Gothic **** W3
Font: HiraKakuProN-W3 Font Family: Hiragino Kaku Gothic **** W6
Font: HiraKakuProN-W6 Font Family: Marker Felt
Font: MarkerFelt-Thin Font Family: STHeiti J
Font: STHeitiJ-Medium
Font: STHeitiJ-Light Font Family: STHeiti K
Font: STHeitiK-Medium
Font: STHeitiK-Light Font Family: STHeiti SC
Font: STHeitiSC-Medium
Font: STHeitiSC-Light Font Family: STHeiti TC
Font: STHeitiTC-Light
Font: STHeitiTC-Medium Font Family: Times New Roman
Font: TimesNewRomanPSMT
Font: TimesNewRomanPS-BoldMT
Font: TimesNewRomanPS-BoldItalicMT
Font: TimesNewRomanPS-ItalicMT Font Family: Trebuchet MS
Font: TrebuchetMS-Italic
Font: TrebuchetMS
Font: Trebuchet-BoldItalic
Font: TrebuchetMS-Bold Font Family: Verdana
Font: Verdana-Bold
Font: Verdana-BoldItalic
Font: Verdana
Font: Verdana-Italic Font Family: Zapfino
Font: Zapfino
*/ //文本对齐方式
/* Values for NSTextAlignment */
/*
NSTextAlignmentLeft 左对齐
NSTextAlignmentCenter 剧中对齐
NSTextAlignmentRight 右对齐
NSTextAlignmentJustified 两端对齐
NSTextAlignmentNatural 根据显示的文字特性对齐
*/ //段落样式
/*
lineSpacing; 来增加行距
paragraphSpacing;
alignment; 对齐
firstLineHeadIndent; 段落开始的缩排像素
headIndent; 可调整全部文字的缩排距离,可当作左边 padding 使用
tailIndent; 可调整文字尾端的缩排距离。需要注意的是,这里指定的值可以当作文字显示的宽、而也可当作右边 padding 使用,依据输入的正负值而定:
lineBreakMode;
minimumLineHeight;
maximumLineHeight; 而针对不同的字型与字号,我们可以透过指定最大与最小行距(maximumLineHeight 与 minimumLineHeight)来避免过高或过窄的状况发生。
baseWritingDirection;
lineHeightMultiple; 想要调整行距,可以透过搭配使用 lineHeightMultiple 更改行距倍数
paragraphSpacingBefore; 而若是文章内容有分段落的话,也可以透过指定段落结尾距离(paragraphSpacing)以及段落开头距离(paragraphSpacingBefore):
hyphenationFactor;
@property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
@property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
*/ /* Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below. //预定义的文本属性值,如果键不是一个字典,那么使用默认的值作为以下描述
NSFontAttributeName 字体 默认是Helvetica 12号
NSParagraphStyleAttributeName 段落样式
*/
/*
UIKIT_EXTERN NSString *const NS_AVAILABLE_IOS(6_0); // NSParagraphStyle, default defaultParagraphStyle
UIKIT_EXTERN NSString *const NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default blackColor
UIKIT_EXTERN NSString *const NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: no background
UIKIT_EXTERN NSString *const NSLigatureAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
UIKIT_EXTERN NSString *const NSKernAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled. (note: values other than nil and 0 are unsupported on iOS)
UIKIT_EXTERN NSString *const NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no strikethrough
UIKIT_EXTERN NSString *const NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no underline
UIKIT_EXTERN NSString *const NSStrokeColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString *const NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
UIKIT_EXTERN NSString *const NSShadowAttributeName NS_AVAILABLE_IOS(6_0); // NSShadow, default nil: no shadow
UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0); // NSString, default nil: no text effect UIKIT_EXTERN NSString *const NSAttachmentAttributeName NS_AVAILABLE_IOS(7_0); // NSTextAttachment, default nil
UIKIT_EXTERN NSString *const NSLinkAttributeName NS_AVAILABLE_IOS(7_0); // NSURL (preferred) or NSString
UIKIT_EXTERN NSString *const NSBaselineOffsetAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value, in points; offset from baseline, default 0
UIKIT_EXTERN NSString *const NSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString *const NSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString *const NSObliquenessAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
UIKIT_EXTERN NSString *const NSExpansionAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion UIKIT_EXTERN NSString *const NSWritingDirectionAttributeName NS_AVAILABLE_IOS(7_0); // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values. LRE: NSWritingDirectionLeftToRight|NSTextWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSTextWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSTextWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSTextWritingDirectionOverride, UIKIT_EXTERN NSString *const NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(6_0); // An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it's always horizontal. The behavior for any other value is undefined.
*/ // NSParagraphStyle 段落样式
// typedef NS_ENUM(NSInteger, NSLineBreakMode) { /* What to do with long lines */ //对于长内容或多行内容的处理方式
// NSLineBreakByWordWrapping = 0, /* Wrap at word boundaries, default */ //按包含单词为界限截断
// NSLineBreakByCharWrapping, /* Wrap at character boundaries */ //按字符为界限截断
// NSLineBreakByClipping, /* Simply clip */ //简单的修剪
// NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */ //截断头部
// NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */ //截断尾巴
// NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz" */ //截断中间
// } NS_ENUM_AVAILABLE_IOS(6_0);
}
//获取斜体
UIFont * GetVariationOfFontWithTrait(UIFont *baseFont,
CTFontSymbolicTraits trait) {
CGFloat fontSize = [baseFont pointSize];
CFStringRef
baseFontName = (__bridge CFStringRef)[baseFont fontName];
CTFontRef baseCTFont = CTFontCreateWithName(baseFontName,
fontSize, NULL);
CTFontRef ctFont =
CTFontCreateCopyWithSymbolicTraits(baseCTFont, 0, NULL,
trait, trait);
NSString *variantFontName =
CFBridgingRelease(CTFontCopyName(ctFont,
kCTFontPostScriptNameKey)); UIFont *variantFont = [UIFont fontWithName:variantFontName
size:fontSize];
CFRelease(ctFont);
CFRelease(baseCTFont);
return variantFont;
}; - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
UIlabel - 富文本属性的更多相关文章
- UILabel富文本 段落格式以及UILabel添加图片
之前文本整理有一点乱,这边重新整理一下,下面是效果图,一共两个UILabel, 富文本整理: /*NSForegroundColorAttributeName设置字体颜色,对象UIColor; NSP ...
- UILabel(富文本)
本文转载至 http://www.jianshu.com/p/5d24d22f99c3 富文本 NSString *str = @"人生若只如初见,何事秋风悲画扇.\n等闲变却故人心,却道故 ...
- iOS 富文本属性
// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12 // NSForegroundColorAttributeNam 设置字体颜色,取 ...
- 简易使用UILabel的富文本
简易使用UILabel的富文本 使用效果: 源码: NSString+YX.h NSString+YX.m // // NSString+YX.h // YXKit // // Copyrigh ...
- UILabel和UIbutton(富文本)封装方法
/** 方法说明:设置label的富文本属性 参数说明:contentStr富文本内容 textColor字体颜色 rangeSet设置字体颜色及大小的位置 */ - (UILabel *)backf ...
- iOS开发富文本制作 图片和文字/NSMutableParagraphStyle/NSMutableAttributedString
/NSMutableParagraphStyle/NSMutableAttributedString 组合使 NSString * titlestr=@"日产GT-R"; NSMu ...
- swift---不同字体大小不同颜色label富文本设置
agreeDeal = UILabel() //富文本,不同字体颜色大小和颜色 let labelString = "登录注册,表示您同意<服务条款及隐私政策>"as ...
- iOS计算富文本(NSMutableAttributedString)高度
有时候开发中我们为了样式好看, 需要对文本设置富文本属性, 设置完后那么怎样计算其高度呢, 很简单, 方法如下: - (NSInteger)hideLabelLayoutHeight:(NSStrin ...
- UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...
随机推荐
- Linux守护进程的编程实现
Linux 守护进程的编程方法 守护进程(Daemon)是执行在后台的一种特殊进程.它独立于控制终端而且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种非常实用的进程.Linux的大多数s ...
- android之自定义ViewGroup和自动换行的布局的实现
viewgroup简单说就是可以装view的view.今天遇到一个问题,就是需要一个可以自动根据一行中view的宽度自动换行的布局,网上 找了下,没有相关的例子,但是找到了思路:自定义一个viewgr ...
- MySQL 数据库常用命令 超级实用版分享
1.MySQL常用命令 create database name; 创建数据库 use databasename; 选择数据库 drop database name 直接删除数据库,不提醒 show ...
- CSS 隐藏多余的字符
日常开发中常常会碰到,字符长度太大,撑破了样式的问题.如果采用截取的话,显然是不灵活的.但是通过css样式来控制显示就比较简单和高效了.下面是关键代码 样式名称{wedth:??px;height=? ...
- Java并发编程:进程和线程之由来__进程让操作系统的并发性成为可能,而线程让进程的内部并发成为可能
转载自海子:http://www.cnblogs.com/dolphin0520/p/3910667.html Java多线程基础:进程和线程之由来 在前面,已经介绍了Java的基础知识,现在我们来讨 ...
- python模块与包
模块是包括python定义和声明的文件.文件名=模块名+".py".模块名保存在全局变量__name__中. 1.模块中的执行语句,只是在导入时执行一次.这些语句通常用于初始化模块 ...
- java新手笔记5 类
1.进制转换 /* 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时, 低于10万元的部分按10%提成,高于10万元的部分,可提成7.5 ...
- spring定时器用Annotation兑现
spring定时器用Annotation实现 0人收藏此文章, 我要收藏发表于3个月前 , 已有46次阅读 共0个评论 1.ApplicationContext.xml配置 a).需要在xmlns里面 ...
- Codevs 2776 寻找代表元(二分图匹配)
2776 寻找代表元 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 广州二中苏元实验学校一共有n个社团,分别用1到n编号. 广州二 ...
- apache config directive – order, allow, deny
在对apache进行配置的时候,常看到oerder, allow, deny.现在就简单回顾一下其用法. 对于每个对于资源的请求,服务器可以配置是否允许这个请求通过.在配置当中,使用的是允许与不允许的 ...