效果图
//
// HXLLinedTextView.m
// HXLLinedTextView
//
// Created by Hao Xuliang on 6/08/13.
// Copyright (c) 2013 Hao Xuliang. All rights reserved.
//
#import "HXLLinedTextView.h"
#define DEFAULT_HORIZONTAL_COLOR [UIColor colorWithRed:0.722f green:0.910f blue:0.980f alpha:0.7f]
#define DEFAULT_VERTICAL_COLOR [UIColor colorWithRed:0.957f green:0.416f blue:0.365f alpha:0.7f]
#define DEFAULT_MARGINS UIEdgeInsetsMake(10.0f,10.0f, 0.0f,10.0f)
@interface HXLLinedTextView()
@property (nonatomic,strong) UIColor *horizontalLineColorUI_APPEARANCE_SELECTOR;
@property (nonatomic,strong) UIColor *verticalLineColorUI_APPEARANCE_SELECTOR;
@property (nonatomic)UIEdgeInsets marginsUI_APPEARANCE_SELECTOR;
@property (nonatomic,assign) UIView *webDocumentView;
@end
@implementation HXLLinedTextView
+ (void)initialize
{
if (self == [HXLLinedTextViewclass])
{
id appearance = [selfappearance];
[appearance setContentMode:UIViewContentModeRedraw];
[appearance setHorizontalLineColor:DEFAULT_HORIZONTAL_COLOR];
[appearancesetVerticalLineColor:DEFAULT_VERTICAL_COLOR];
[appearancesetMargins:DEFAULT_MARGINS];
}
}
#pragma mark - Superclass overrides
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self)
{
UIFont *font = self.font;
self.font =nil;
self.font = font;
self.webDocumentView = [self.subviewsobjectAtIndex:0];
self.margins = [self.class.appearancemargins];
}
return self;
}
- (void)setContentSize:(CGSize)contentSize
{
contentSize = (CGSize) {
.width = contentSize.width -self.margins.left -self.margins.right,
.height =MAX(contentSize.height,self.bounds.size.height -self.margins.top)
};
self.webDocumentView.frame = (CGRect) {
.origin = self.webDocumentView.frame.origin,
.size = contentSize
};
[supersetContentSize:contentSize];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,1.0f);
if (self.horizontalLineColor)
{
//开始绘制 水平线
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context,self.horizontalLineColor.CGColor);
CGFloat baseOffset = 7.0f + self.font.descender;
CGFloat screenScale = [UIScreenmainScreen].scale;
CGFloat boundsX = self.bounds.origin.x;
CGFloat boundsWidth = self.bounds.size.width;
NSInteger firstVisibleLine = MAX(1, (self.contentOffset.y /self.font.lineHeight));
NSInteger lastVisibleLine =ceilf((self.contentOffset.y +self.bounds.size.height) /self.font.lineHeight);
for (NSInteger line = firstVisibleLine; line <= lastVisibleLine; ++line)
{
CGFloat linePointY = (baseOffset + (self.font.lineHeight * line));
CGFloat roundedLinePointY = roundf(linePointY * screenScale) / screenScale;
CGContextMoveToPoint(context, boundsX, roundedLinePointY);
CGContextAddLineToPoint(context, boundsWidth, roundedLinePointY);
}
//结束绘制水平线
CGContextClosePath(context);
CGContextStrokePath(context);
}
if (self.verticalLineColor)
{
//开始绘制 垂直线
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context,self.verticalLineColor.CGColor);
CGContextMoveToPoint(context, -1.0f,self.contentOffset.y);
CGContextAddLineToPoint(context, -1.0f,self.contentOffset.y +self.bounds.size.height);
//结束绘制 垂直线
CGContextClosePath(context);
CGContextStrokePath(context);
}
}
- (void)setFont:(UIFont *)font
{
[supersetFont:font];
[selfsetNeedsDisplay];
}
#pragma mark - Property methods
- (void)setHorizontalLineColor:(UIColor *)horizontalLineColor
{
_horizontalLineColor = horizontalLineColor;
[selfsetNeedsDisplay];
}
- (void)setVerticalLineColor:(UIColor *)verticalLineColor
{
_verticalLineColor = verticalLineColor;
[selfsetNeedsDisplay];
}
- (void)setMargins:(UIEdgeInsets)margins
{
_margins = margins;
self.contentInset = (UIEdgeInsets) {
.top =self.margins.top,
.left =self.margins.left,
.bottom =self.margins.bottom,
.right =self.margins.right -self.margins.left
};
[selfsetContentSize:self.contentSize];
}
@end