可变字符串改变颜色大小
2016-03-14 17:37
387 查看
自己封装一个类
@interface NSStringModify :
NSObject
+ (NSMutableAttributedString *)changeFontString:(NSString *)string Font1:(CGFloat)font1 Font2:(CGFloat)font2;
@end
@implementation NSStringModify
+ (NSMutableAttributedString *)changeFontString:(NSString *)string Font1:(CGFloat)font1 Font2:(CGFloat)font2 {
NSRange range1 =
NSMakeRange(0, 1);
NSRange range2 =
NSMakeRange(2, string.length - 2);
NSMutableAttributedString *str = [[NSMutableAttributedString
alloc]
initWithString:string];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor
redColor]
range:range1];
[str addAttribute:NSFontAttributeName
value:[UIFont
systemFontOfSize:font1] range:range1];
[str addAttribute:NSFontAttributeName
value:[UIFont
systemFontOfSize:font2] range:range2];
return str;
}
//调用
self.label = [[UILabel
alloc] initWithFrame:CGRectMake(100,
100,
150, 100)];
NSString *string =
@"今天自己没吃药,感觉自己萌萌哒~";
self.label.numberOfLines =
3;
self.label.attributedText
= [NSStringModify
changeFontString:string Font1:14
Font2:18];
[self.view
addSubview:self.label];
@end
@interface NSStringModify :
NSObject
+ (NSMutableAttributedString *)changeFontString:(NSString *)string Font1:(CGFloat)font1 Font2:(CGFloat)font2;
@end
@implementation NSStringModify
+ (NSMutableAttributedString *)changeFontString:(NSString *)string Font1:(CGFloat)font1 Font2:(CGFloat)font2 {
NSRange range1 =
NSMakeRange(0, 1);
NSRange range2 =
NSMakeRange(2, string.length - 2);
NSMutableAttributedString *str = [[NSMutableAttributedString
alloc]
initWithString:string];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor
redColor]
range:range1];
[str addAttribute:NSFontAttributeName
value:[UIFont
systemFontOfSize:font1] range:range1];
[str addAttribute:NSFontAttributeName
value:[UIFont
systemFontOfSize:font2] range:range2];
return str;
}
//调用
self.label = [[UILabel
alloc] initWithFrame:CGRectMake(100,
100,
150, 100)];
NSString *string =
@"今天自己没吃药,感觉自己萌萌哒~";
self.label.numberOfLines =
3;
self.label.attributedText
= [NSStringModify
changeFontString:string Font1:14
Font2:18];
[self.view
addSubview:self.label];
@end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- C#获取文件夹及文件的大小与占用空间的方法
- 减小VC6编译生成的exe文件的大小的方法
- JavaScript限定图片显示大小的方法
- JavaScript获取元素尺寸和大小操作总结
- Delphi实现获取磁盘空间大小的方法
- JS+css 图片自动缩放自适应大小
- Javascript获取窗口(容器)的大小及位置参数列举及简要说明
- PHP 上传文件大小限制
- javascript 客户端验证上传图片的大小(兼容IE和火狐)
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法