您的位置:首页 > 产品设计 > UI/UE

可变字符串改变颜色大小

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息