您的位置:首页 > 移动开发 > IOS开发

ios富文本属性

2016-05-16 09:09 405 查看
@interface
ViewController ()@property
(weak,nonatomic)

IBOutlet
UILabel
*stringLabel;@end@implementation
ViewController

- (void)viewDidLoad
{

[super
viewDidLoad];

NSString
*string = _stringLabel.text;
//初始化属性字符串

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
//字体类型属性

NSDictionary
*BoldFontAS = @{NSFontAttributeName:[UIFont
boldSystemFontOfSize:17]};
[attributedString addAttributes:BoldFontAS range:[string rangeOfString:@"BoldFont"]];
//字体颜色属性

NSDictionary
*RedFondAS = @{NSForegroundColorAttributeName :[UIColor
redColor]};

[attributedString addAttributes:RedFondAS range:[string rangeOfString:@"RedFont"]];
//字体背景颜色和字体颜色属性
NSDictionary
*BuleBackgroundAS = @{NSBackgroundColorAttributeName:[UIColor
blueColor], NSForegroundColorAttributeName:[UIColor
whiteColor]};

[attributedString addAttributes:BuleBackgroundAS range:[string rangeOfString:@"BuleBackground"]];
//字体下划线与字体下划线颜色属性

NSDictionary
*UnderlineAS = @{NSUnderlineStyleAttributeName:[NSNumber
numberWithInteger:NSUnderlineStyleSingle],NSUnderlineColorAttributeName:[UIColor
greenColor]};

[attributedString addAttributes:UnderlineAS range:[string rangeOfString:@"Underline"]];
//字体阴影属性

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

shadow.shadowOffset
= CGSizeMake(2,
2);

shadow.shadowColor
= [UIColor
orangeColor];

NSDictionary
*ShadowAS = @{NSShadowAttributeName:shadow};

[attributedString addAttributes:ShadowAS range:[string rangeOfString:@"Shadow"]];
//设置Label的字符串属性
_stringLabel.attributedText
= attributedString;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: