gpt4 book ai didi

iphone - 在 iPhone 的 UITextView 上绘制规则线

转载 作者:行者123 更新时间:2023-12-03 19:04:18 25 4
gpt4 key购买 nike

我想创建一个像 iPhone 上的笔记应用程序一样的 View ,因此需要该 View 按照笔记应用程序具有规则线,我已经在 Windows 中完成了此操作,您需要获取字体规范,然后绘制线条到设备上下文中,有没有人在 UITextView 中完成此操作,如果是的话,会提供一些帮助

最佳答案

UITextView 的子类。覆盖-drawRect:

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetLineWidth(context, self.lineWidth);
CGFloat strokeOffset = (self.lineWidth / 2);

CGFloat rowHeight = self.font.lineHeight;
if (rowHeight > 0) {
CGRect rowRect = CGRectMake(self.contentOffset.x, - self.bounds.size.height, self.contentSize.width, rowHeight);
while (rowRect.origin.y < (self.bounds.size.height + self.contentSize.height)) {
CGContextMoveToPoint(context, rowRect.origin.x + strokeOffset, rowRect.origin.y + strokeOffset);
CGContextAddLineToPoint(context, rowRect.origin.x + rowRect.size.width + strokeOffset, rowRect.origin.y + strokeOffset);
CGContextDrawPath(context, kCGPathStroke);
rowRect.origin.y += rowHeight;
}
}
}

初始化 TextView 时,请务必将 contentMode 设置为 UIViewContentModeRedraw。否则,行将不会随文本滚动。

self.contentMode = UIViewContentModeRedraw;

这并不完美。理想情况下,您应该只绘制经过的矩形。但我很懒,这满足了我的需要。

关于iphone - 在 iPhone 的 UITextView 上绘制规则线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3492045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com