iOS-屬性字符串NSAttributedString

關(guān)于NSAttributedString只記得幾個常用的屬性,有時候要用得特殊的屬性的時候就得到處去翻,在這里記錄一下闲先,以后要用的時候就不用到處去找了羞酗。

一個簡單的例子

繪制不同顏色不同字體的一個AttributeString,如圖

1.png

代碼如下:

 UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * firstPart = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * firstAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],};
    [firstPart setAttributes:firstAttributes range:NSMakeRange(0,firstPart.length)];
    NSMutableAttributedString * secondPart = [[NSMutableAttributedString alloc] initWithString:@" Huang"];
    NSDictionary * secondAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor blueColor],};
    [secondPart setAttributes:secondAttributes range:NSMakeRange(0,secondPart.length)];
    [firstPart appendAttributedString:secondPart];
    Label.attributedText = firstPart;

先看看所有的Key

NSFontAttributeName; //字體腐宋,value是UIFont對象
NSParagraphStyleAttributeName;//繪圖的風(fēng)格(居中,換行模式整慎,間距等諸多風(fēng)格)脏款,value是NSParagraphStyle對象
NSForegroundColorAttributeName;// 文字顏色,value是UIFont對象
NSBackgroundColorAttributeName;// 背景色裤园,value是UIFont
NSLigatureAttributeName; //  字符連體撤师,value是NSNumber
NSKernAttributeName; // 字符間隔
NSStrikethroughStyleAttributeName;//刪除線,value是NSNumber
NSUnderlineStyleAttributeName;//下劃線拧揽,value是NSNumber
NSStrokeColorAttributeName; //描繪邊顏色剃盾,value是UIColor
NSStrokeWidthAttributeName; //描邊寬度腺占,value是NSNumber
NSShadowAttributeName; //陰影,value是NSShadow對象
NSTextEffectAttributeName; //文字效果痒谴,value是NSString
NSAttachmentAttributeName;//附屬衰伯,value是NSTextAttachment 對象
NSLinkAttributeName;//鏈接,value是NSURL or NSString
NSBaselineOffsetAttributeName;//基礎(chǔ)偏移量积蔚,value是NSNumber對象
NSUnderlineColorAttributeName;//下劃線顏色意鲸,value是UIColor對象
NSStrikethroughColorAttributeName;//刪除線顏色,value是UIColor
NSObliquenessAttributeName; //字體傾斜
NSExpansionAttributeName; //字體扁平化
NSVerticalGlyphFormAttributeName;//垂直或者水平尽爆,value是 NSNumber怎顾,0表示水平,1垂直

字體漱贱,顏色槐雾,背景色

涉及到的屬性

- NSFontAttributeName 
- NSForegroundColorAttributeName 
- NSBackgroundColorAttributeName

效果

0.png

代碼

UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:14]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;

下劃線

涉及到的兩個屬性

NSUnderlineStyleAttributeName
NSUnderlineColorAttributeName

效果

0.png

代碼

    UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],                          NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),                             NSUnderlineColorAttributeName:[UIColor blueColor],};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;

描邊

涉及到的兩個屬性

NSStrokeColorAttributeName
NSStrokeWidthAttributeName

效果

0.png

代碼

    UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSStrokeColorAttributeName:[UIColor greenColor],NSStrokeWidthAttributeName:@(2)};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;

附屬屬性(例如圖片)

涉及到的屬性

NSAttachmentAttributeName

效果

0.png

代碼

  UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    //創(chuàng)建Attachment Str
    NSTextAttachment * attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:@"memoAccess"];
    attach.bounds = CGRectMake(0, 0, 20, 20);
    NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];
    //添加
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    [mutableAttriStr appendAttributedString:imageStr];
    Label.attributedText = mutableAttriStr;

繪制風(fēng)格

為什么要紅字?因?yàn)檫@個屬性真的很重要幅狮。

涉及到的屬性

NSMutableParagraphStyle

效果(看起來很奇怪募强,僅僅為了展示某些功能)

20150703172550840.png

代碼

@interface TestView : UIView

@end

@implementation TestView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    self.opaque = NO;
    return self;
}

// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@"The anthor of this blog is wenchenhuang"];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentRight;
    paragraphStyle.headIndent = 4.0;
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.lineSpacing = 2.0;
    NSDictionary * attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
    [attributeStr setAttributes:attributes range:NSMakeRange(0, attributeStr.length)];
    [attributeStr drawInRect:self.bounds];
}

    TestView  *test = [[TestView alloc] initWithFrame:CGRectMake(100, 100,100, 60)];
    [self.view addSubview:test];

陰影

涉及到的屬性

NSShadowAttributeName

效果

20150705143210722.png
  UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSShadow * shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor blueColor];
    shadow.shadowBlurRadius = 2.0;
    shadow.shadowOffset = CGSizeMake(1.0, 1.0);
    NSDictionary * attris = @{NSShadowAttributeName:shadow};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;

文字效果

相關(guān)屬性

NSTextEffectAttributeName 
20150705143535404.png

代碼

 UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;

鏈接

相關(guān)屬性

NSLinkAttributeName

例子

點(diǎn)擊打開鏈接

@interface ViewController ()<UITextViewDelegate>

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    textView.scrollEnabled = NO;
    textView.editable = NO;
    textView.textContainer.lineFragmentPadding = 0;
    textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    textView.delegate = self;
    [self.view addSubview:textView];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    textView.attributedText = mutableAttriStr;
        // Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
{
    return YES;
}

文字連體

涉及到的屬性

NSLigatureAttributeName

舉例
NSLigatureAttributeName 屬性位@(0) 和@(1)

20150705145903705.png

字符間隔

相關(guān)屬性

NSKernAttributeName

舉例 字符間距拉大到4

20150705150830533.png

對比下,默認(rèn)的

20150705150859360.png
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 120)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSKernAttributeName:@(4),
                              NSFontAttributeName:[UIFont systemFontOfSize:30]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;

baseline基礎(chǔ)偏移量

相關(guān)屬性

- NSBaselineOffsetAttributeName

效果
對比下偏移量為0 和20

20150705151401144.png

代碼

  NSDictionary * attris = @{NSBaselineOffsetAttributeName:@(0),
                              NSFontAttributeName:[UIFont systemFontOfSize:30]};

字體傾斜

相關(guān)屬性

NSObliquenessAttributeName 

效果

20150705152223389.png

代碼

    NSDictionary * attris = @{NSObliquenessAttributeName:@(0.5),
                              NSFontAttributeName:[UIFont systemFontOfSize:30]};```
字體扁平化

相關(guān)屬性

NSExpansionAttributeName

效果 

![](http://upload-images.jianshu.io/upload_images/3065850-aa885bf9c6346803.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

代碼

NSDictionary * attris = @{NSExpansionAttributeName:@(1.0),
NSFontAttributeName:[UIFont systemFontOfSize:30]};

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末崇摄,一起剝皮案震驚了整個濱河市擎值,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌配猫,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泵肄,死亡現(xiàn)場離奇詭異,居然都是意外死亡腐巢,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門冯丙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肉瓦,“玉大人,你說我怎么就攤上這事泞莉〈常” “怎么了鲫趁?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長利虫。 經(jīng)常有香客問我挨厚,道長,這世上最難降的妖魔是什么疫剃? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮牲阁,結(jié)果婚禮上蹄溉,老公的妹妹穿的比我還像新娘咨油。我一直安慰自己柒爵,他們只是感情好赚爵,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著冀膝,像睡著了一般。 火紅的嫁衣襯著肌膚如雪麻掸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天脊奋,我揣著相機(jī)與錄音疙描,去河邊找鬼。 笑死起胰,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的地消。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼脉执,長吁一口氣:“原來是場噩夢啊……” “哼瓜客!你這毒婦竟也來了竿开?” 一聲冷哼從身側(cè)響起玻熙,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嗦随,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體枚尼,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年崎溃,在試婚紗的時候發(fā)現(xiàn)自己被綠了盯质。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡囱修,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出王悍,到底是詐尸還是另有隱情,我是刑警寧澤鲜漩,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布渠脉,位于F島的核電站,受9級特大地震影響芋膘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜为朋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望胶惰。 院中可真熱鬧,春花似錦孵滞、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽匿级。三九已至,卻和暖如春痘绎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背尔苦。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工散庶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人悲龟。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓冰寻,卻偏偏與公主長得像须教,于是被迫代替她去往敵國和親斩芭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,074評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件琴庵、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評論 4 62
  • 這次出差專程從洛陽轉(zhuǎn)車,主要是想看看洛陽博物館儿礼,但是買了車票后我才想起好多博物館都是周一閉館庆寺,一查蚊夫,洛陽博物館也是...
    寒塘渡鶴閱讀 1,328評論 8 4
  • 熊志軍~【日精進(jìn)打卡第460天】 8月17號卡 付達(dá)新商貿(mào)~眾德營銷 沈陽盛和塾道盛組/稻芽七組 【知~學(xué)習(xí)】 早...
    熊志軍閱讀 154評論 0 1
  • 在項(xiàng)目根目錄下打開命令窗口 輸入 java –jar myjar.jar 啟動myjar.jar
    zhuxuewen閱讀 426評論 0 0