#import "EHTextView.h" @implementation EHTextView - (instancetype)initWithFrame:(CGRect)frame andText:(NSString *)mText { self = [super initWithFrame:frame]; if (self) { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; // 行间距 paragraphStyle.lineHeightMultiple = 40; paragraphStyle.maximumLineHeight = 40; paragraphStyle.minimumLineHeight = 40; self.textContainerInset = UIEdgeInsetsMake(-10, 0, 0, 0); NSString *oneRowStr = [mText substringWithRange:NSMakeRange(0, [mText rangeOfString:@" "].location)]; NSMutableAttributedString *prefixAttr = [[NSMutableAttributedString alloc] initWithString:mText]; [prefixAttr addAttribute:NSFontAttributeName value:FZHei(17) range:NSMakeRange(0, mText.length)]; [prefixAttr addAttribute:NSForegroundColorAttributeName value:ColorHui range:NSMakeRange(0, mText.length)]; [prefixAttr addAttribute:NSFontAttributeName value:FZHei(17) range:NSMakeRange(0, oneRowStr.length)]; [prefixAttr addAttribute:NSForegroundColorAttributeName value:ColorHuiHei range:NSMakeRange(0, oneRowStr.length)]; [prefixAttr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mText.length)]; self.attributedText = prefixAttr; self.backgroundColor = [UIColor clearColor]; self.userInteractionEnabled = NO; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, HexRGB(0xd2d2d2).CGColor); CGContextSetLineWidth(context, 0.5f); CGContextBeginPath(context); CGFloat rowH = 40; NSUInteger numberOfLines = (self.bounds.size.height - 15) / rowH; CGFloat baselineOffset = 0; for (int x = 1; x < numberOfLines + 1; x++) { CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor); CGFloat lengthstest[] = {5,2}; CGContextSetLineDash(context, 2, lengthstest,1); CGContextMoveToPoint(context, 2, rowH * x + baselineOffset); CGFloat rowW = numberOfLines == x ? 35 : 0; CGContextAddLineToPoint(context, CGRectGetWidth(rect) - rowW, rowH * x + baselineOffset); CGContextStrokePath(context); } CGContextStrokePath(context); }
@end