NSAttributeString创建各种文字效果

本文通过多个实例介绍了NSAttributedString在iOS开发中的应用技巧,包括字体颜色、背景色、阴影效果、段落样式等属性设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[objc]  view plain  copy
  1. NSDictionary *attributes =@{  
  2. NSForegroundColorAttributeName: [UIColorredColor],  
  3. NSFontAttributeName: [UIFontfontWithName:@"Zapfino" size:16.0]  
  4.   
  5.     };  
  6.     NSString *strDisplayText =@"This is an attributed string.";  
  7.     NSAttributedString *attributedText = [[NSAttributedStringalloc] initWithString:strDisplayTextattributes:attributes];  
  8.     self.lblInfo.attributedText= attributedText;  



[objc]  view plain  copy
  1. NSDictionary *attributes1 =@{  
  2. NSBackgroundColorAttributeName: [UIColororangeColor],  
  3.     NSFontAttributeName: [UIFontfontWithName:@"Zapfino" size:22.0],  
  4. NSKernAttributeName: @-1.0  
  5.     };  
  6.     NSString *strDisplayText1 =@"Orange Background";  
  7.     NSAttributedString *attributedText1 = [[NSAttributedStringalloc] initWithString:strDisplayText1attributes:attributes1];  
  8.     self.lblInfo1.attributedText= attributedText1;  




[objc]  view plain  copy
  1. NSShadow*shadow = [[NSShadow alloc]init];  
  2.     shadow.shadowColor = [UIColorgreenColor];  
  3.     shadow.shadowBlurRadius = 5.0;  
  4.     shadow.shadowOffset = CGSizeMake(1.0,1.0);  
  5.     NSDictionary *attributes2 =@{  
  6. NSUnderlineStyleAttributeName:@1,  
  7. NSShadowAttributeName: shadow  
  8.     };  
  9.     NSString *strDisplayText2 =@"Shadow Font";  
  10.     NSAttributedString *attributedText2 = [[NSAttributedStringalloc] initWithString:strDisplayText2attributes:attributes2];  
  11.     self.lblInfo2.attributedText= attributedText2;  





[objc]  view plain  copy
  1. NSDictionary*subStrAttribute1 = @{  
  2. NSForegroundColorAttributeName: [UIColorredColor],  
  3. NSStrikethroughStyleAttributeName:@2  
  4.     };  
  5.      
  6.     NSDictionary *subStrAttribute2 =@{  
  7. NSForegroundColorAttributeName: [UIColorgreenColor]  
  8.     };  
  9.      
  10.     NSString *strDisplayText3 =@"Red and Green";  
  11.     NSMutableAttributedString *attributedText3 = [[NSMutableAttributedStringalloc] initWithString:strDisplayText3];  
  12.     [attributedText3 setAttributes:subStrAttribute1range:NSMakeRange(0,3)];  
  13.     [attributedText3 setAttributes:subStrAttribute2range:NSMakeRange(8,5)];  
  14.     self.lblInfo3.attributedText= attributedText3;  



[objc]  view plain  copy
  1. //段落样式设置  
  2. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];  
  3.     paragraph.alignment = NSTextAlignmentJustified;  
  4.     paragraph.firstLineHeadIndent =20.0;  
  5.     paragraph.paragraphSpacingBefore = 10.0;  
  6.     paragraph.lineSpacing = 5;  
  7.     paragraph.hyphenationFactor =1.0;  
  8.      
  9.     NSDictionary *attributes4 =@{  
  10. NSForegroundColorAttributeName: [UIColorredColor],  
  11. NSParagraphStyleAttributeName: paragraph  
  12.     };  
  13.      
  14.     NSString *strDisplayText4 =@“iPad inspires creativity and ……”;  
  15.     NSAttributedString *attributedText4 = [[NSAttributedStringalloc] initWithString: strDisplayText4attributes:attributes4];  
  16.     self.lblInfo4.attributedText= attributedText4;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值