
给文本添加富文本
- 设置富文本富文本:addAttributes
- 富文本拼接:appendAttributedString
1.阴影1
2
3
4
5
6
7
8
9NSMutableAttributedString *text = [NSMutableAttributedString new];
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Shadow"];
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowBlurRadius = 5;
shadow.shadowOffset = CGSizeMake(0, 1);
shadow.shadowColor = [UIColor colorWithWhite:0.000 alpha:0.490];
[one addAttributes:@{NSShadowAttributeName:shadow,NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:30]} range:NSMakeRange(0, 6)];
[text appendAttributedString:one];
label.attributedText = text;
2.空心字
1 |
[one addAttributes:@{NSStrokeColorAttributeName:[UIColor redColor],NSStrokeWidthAttributeName:@1.5} range:NSMakeRange(0, 6)]; |
3.特殊效果
1 |
[one addAttributes:@{NSTextEffectAttributeName:NSTextEffectLetterpressStyle,} range:NSMakeRange(0, 6)]; |
4.下划线、中划线
-
下划线:NSUnderlineColorAttributeName
1
[one addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSUnderlineColorAttributeName:[UIColor redColor]];
-
中划线
1
[one addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle),NSStrikethroughColorAttributeName:[UIColor greenColor]} range:NSMakeRange(0, 6)];
线样式:
1 |
typedef NS_ENUM(NSInteger, NSUnderlineStyle) { |
5.链接
- label上的链接点击不了,textView可以,回调函数
shouldInteractWithURL中可以响应。
1
[one addAttributes:@{NSLinkAttributeName:@"https://www.baidu.com"} range:NSMakeRange(0, 6)];
6.段落
1 |
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init]; |
7.图文混排
1 |
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"红红火火恍恍惚惚"]; |




近期评论