创建带分隔线的UITextView——像笔记本一样

本文介绍如何创建一个类似笔记本的带行分隔线的自定义UITextView,通过HXLLinedTextView类实现,包括设置横线、竖线颜色及边距的方法,并提供了代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图


//

//  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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值