AttributedLabel.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // AttributedLabel.m
  3. // AttributedStringTest
  4. //
  5. // Created by sun huayu on 13-2-19.
  6. // Copyright (c) 2013年 sun huayu. All rights reserved.
  7. //
  8. #import "AttributedLabel.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. @interface AttributedLabel(){
  11. UIImageView *imageView;
  12. CGRect strikeRect;
  13. }
  14. @end
  15. @implementation AttributedLabel
  16. @synthesize attString = _attString;
  17. - (void)dealloc
  18. {
  19. [_attString release];
  20. [super dealloc];
  21. }
  22. - (id)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. // Initialization code
  27. }
  28. return self;
  29. }
  30. - (id)initWithFrame:(CGRect)frame strikethroughPosition:(CGRect) _strikeRect
  31. {
  32. self = [super initWithFrame:frame];
  33. if (self) {
  34. strikeRect = _strikeRect;
  35. }
  36. return self;
  37. }
  38. - (void)drawRect:(CGRect)rect{
  39. CATextLayer *textLayer = [CATextLayer layer];
  40. textLayer.string = _attString;
  41. textLayer.transform = CATransform3DMakeScale(0.5,0.5,1);
  42. textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  43. CGRect lineRect = CGRectMake(strikeRect.origin.x, strikeRect.origin.y, strikeRect.size.width, 1.0);
  44. CGContextRef context = UIGraphicsGetCurrentContext();
  45. // CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
  46. CGContextFillRect(context, lineRect);
  47. [self.layer addSublayer:textLayer];
  48. }
  49. - (void)setText:(NSString *)text{
  50. [super setText:text];
  51. if (text == nil) {
  52. self.attString = nil;
  53. }else{
  54. self.attString = [[[NSMutableAttributedString alloc] initWithString:text] autorelease];
  55. }
  56. }
  57. // 设置某段字的颜色
  58. - (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
  59. if (location < 0||location>self.text.length-1||length+location>self.text.length) {
  60. return;
  61. }
  62. [_attString addAttribute:(NSString *)kCTForegroundColorAttributeName
  63. value:(id)color.CGColor
  64. range:NSMakeRange(location, length)];
  65. }
  66. // 设置某段字的字体
  67. - (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
  68. if (location < 0||location>self.text.length-1||length+location>self.text.length) {
  69. return;
  70. }
  71. [_attString addAttribute:(NSString *)kCTFontAttributeName
  72. value:(id)CTFontCreateWithName((CFStringRef)font.fontName,
  73. font.pointSize*2,
  74. NULL)
  75. range:NSMakeRange(location, length)];
  76. }
  77. // 设置某段字的风格
  78. - (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{
  79. if (location < 0||location>self.text.length-1||length+location>self.text.length) {
  80. return;
  81. }
  82. [_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
  83. value:(id)[NSNumber numberWithInt:style]
  84. range:NSMakeRange(location, length)];
  85. }
  86. /*
  87. // Only override drawRect: if you perform custom drawing.
  88. // An empty implementation adversely affects performance during animation.
  89. - (void)drawRect:(CGRect)rect
  90. {
  91. // Drawing code
  92. }
  93. */
  94. @end