码迷,mamicode.com
首页 > 其他好文 > 详细

属性字符串的一些使用

时间:2017-03-27 15:33:36      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:actor   radius   eal   graphs   features   opera   his   mutable   5.0   

1.了解NSAttributedString类

NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Zapfino" size:16.0]};

    NSString *strDisplayText = @"this is an attributed string .";

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:strDisplayTextattributes:attributes];

    self.lblInfo.attributedText = attributedText;

2.设置字间距和字体背景色

NSDictionary *attributes1 = @{NSBackgroundColorAttributeName: [UIColor orangeColor],//只有在有字的的部分有橘黄色的背景

                                  NSFontAttributeName:[UIFont fontWithName:@"Zapfino" size:22.0],

                                  NSKernAttributeName:@-3.0};//指定了每个字母之间的距离(数值越小字间的距离也越紧密)

    NSString *strDisplayText1 = @"this is an attrubuted string.";

    NSAttributedString *attrubutedText1 = [[NSAttributedString alloc] initWithString:strDisplayText1attributes:attributes1];

 

    self.lblInfo1.attributedText = attrubutedText1;

3.设置阴影和下划线效果

NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowColor = [UIColor greenColor];

    shadow.shadowBlurRadius = 5.0;

    shadow.shadowOffset = CGSizeMake(1.0, 1.0);

    NSDictionary *attributes2 =@{NSUnderlineStyleAttributeName: @1,

                                 NSShadowAttributeName: shadow};

    NSString *strDisplayText2 = @"Shadow Font";

    NSAttributedString *attributedText2 = [[NSAttributedString alloc] initWithString:strDisplayText2attributes:attributes2];

 

    self.lblInfo2.attributedText = attributedText2;

4.为字符串不同部分设置不同效果

NSDictionary *subStrAttribute1 = @{NSForegroundColorAttributeName: [UIColor redColor],

                                       NSStrikethroughStyleAttributeName: @2};

    NSDictionary *subStrAttribute2 = @{NSForegroundColorAttributeName: [UIColor greenColor]};

    NSString *strDisplayText3 = @"red and green ";

    NSMutableAttributedString *attributedText3 = [[NSMutableAttributedString alloc]initWithString:strDisplayText3];

    [attributedText3 setAttributes:subStrAttribute1 range:NSMakeRange(0, 3)];

    [attributedText3 setAttributes:subStrAttribute2 range:NSMakeRange(8, 5)];

 

    self.lblInfo3.attributedText = attributedText3;

5.设置段落效果

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];

    paragraph.alignment = NSTextAlignmentJustified;//分散式对齐

    paragraph.firstLineHeadIndent = 16.0;//每一个段的文字向右缩进16个点

    paragraph.paragraphSpacingBefore = 20;//设置段与段之间的距离为20个点

    paragraph.lineSpacing = 5;//设置行与行之间的距离

    paragraph.hyphenationFactor = 1.0;//设置每行的最后单词是否截断,在0.0-1.0之间,默认为0.0,越接近1.0单词被截断的可能性越大,

    

    NSDictionary *attributes4 = @{NSForegroundColorAttributeName: [UIColor redColor],

                                  NSParagraphStyleAttributeName:paragraph};

    NSString *subDisplayText4 = @"ipad inspires creativity and hands-on learning with features you won‘t find in any other educationla tool - on a device that students really want to use.\nPowerful apps from the app store like itunes u and ibooks let students engage with content in interactive ways,fin  information in an instant, and access an entire library wherever they go.";

//    NSAttributedString *attributedText4 = [[NSAttributedString alloc] initWithString:subDisplayText4];

    NSAttributedString *attributedText4 = [[NSAttributedString alloc] initWithString:subDisplayText4attributes:attributes4];

 

    self.lblInfo4.attributedText = attributedText4;

以下的截图就是对于的效果:

技术分享
好用的字体网址:http://www.ios6-fonts.com/

属性字符串的一些使用

标签:actor   radius   eal   graphs   features   opera   his   mutable   5.0   

原文地址:http://www.cnblogs.com/VzhanggengD/p/6626847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!