查看: 825|回复: 0

[其他分享] iOS实现文字转化成彩色文字图片


16-5-18 13:44:30 | [复制链接]

20

主题

197

帖子

454

积分

牛人

Rank: 3Rank: 3

发表于 16-5-18 13:44:30 | 显示全部楼层 |阅读模式
  iOS实现文字转化成彩色文字图片

  1. -(void)setupContentView
  2. {
  3. UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, ScreenHight - 44 -300)];
  4. [self.view addSubview:contentView];
  5. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
  6. [contentView addGestureRecognizer:tapGesture];
  7. self.topContentView = contentView;
  8. self.topContentView.backgroundColor = [UIColor clearColor];
  9. UILabel *label = [[UILabel alloc] init];
  10. label.numberOfLines = 0;
  11. label.text = @"ABC";
  12. label.frame = [self calculateContextLabelFrameWithTitle:@"ABC"];
  13. label.center = CGPointMake(contentView.bounds.size.width / 2, contentView.bounds.size.height / 2);
  14. label.font = [UIFont fontWithName:self.fontNameArray[0] size:25];
  15. label.textAlignment = NSTextAlignmentCenter;
  16. [contentView addSubview:label];
  17. label.backgroundColor = [UIColor clearColor];
  18. self.contentLabel = label;
  19. self.gradientLayer = [CAGradientLayer layer];
  20. self.gradientLayer.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
  21. self.gradientLayer.backgroundColor = [UIColor clearColor].CGColor;
  22. self.gradientLayer.startPoint = CGPointMake(0,0.5);
  23. self.gradientLayer.endPoint = CGPointMake(1,0.5);
  24. self.gradientLayer.colors = self.grandentArr[0];
  25. [contentView.layer addSublayer:self.gradientLayer];
  26. self.gradientLayer.mask = self.contentLabel.layer;
  27. self.contentLabel.frame = self.gradientLayer.bounds;
  28. }
复制代码
  当输入的文字改变时,重新计算 self.gradientLayer的frame



  1. -(void)textFieldTextChange:(NSNotification*)notice
  2. {
  3. self.contentLabel.text = self.textField.text;
  4. [self reCalculateGradientLayerFrame];
  5. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

订阅|小黑屋|手机版|千锋教育论坛 ( 京ICP备12003911号-3

GMT+8, 19-11-17 12:56 , Processed in 0.349349 second(s), 40 queries .

Powered by Discuz! X3.2

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表