美文网首页
ios 同一label 局部字体加粗

ios 同一label 局部字体加粗

作者: 贾代表 | 来源:发表于2017-11-01 19:55 被阅读2018次
富文本.png

这只是一个简单的demo,用相同的思路也可以构建炫酷的特效,这里就展示最容易的入门代码

代码如下:

  • (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
    label.text = @"Label Text Content, This is a text label things attribute";//默认为空
    label.font = [UIFont systemFontOfSize:17];//默认使用系统的17
    label.textColor = [UIColor orangeColor];//默认使用文本黑色
    label.textAlignment = NSTextAlignmentCenter;//默认是左对齐
    [self.view addSubview:label];

    NSString *string = label.text;
    const CGFloat fontSize = 16.0;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    NSUInteger length = [string length];
    //设置字体
    UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
    [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//设置所有的字体
    UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
    [attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//设置Text这四个字母的字体为粗体
    label.attributedText = attrString;

    // Do any additional setup after loading the view, typically from a nib.
    }
    对你有帮助的话,甩个赞👍吧

相关文章

网友评论

      本文标题:ios 同一label 局部字体加粗

      本文链接:https://www.haomeiwen.com/subject/gragpxtx.html