属性字符串的一些使用

Posted tiankongzhicheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了属性字符串的一些使用相关的知识,希望对你有一定的参考价值。

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 [email protected]{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/


以上是关于属性字符串的一些使用的主要内容,如果未能解决你的问题,请参考以下文章

数组一些细节

我所不知的 javascript Object 的一些现象

Swift 为文件的属性设置了一些字符串值

selenium模糊匹配控件

C# - 使用属性名称作为字符串由属性排序的代码

C# - 使用属性名称作为字符串按属性排序的代码[重复]