创建水平线

发布于 2024-08-25 04:35:48 字数 68 浏览 3 评论 0原文

我有两个堆叠在一起的标签。如果我想要它们之间有一条水平线,除了使用带有 UIImageView 的图像之外还有其他方法吗?

I have two labels that are stacked. If I want a horizontal line between them, is there any other way besides using an image with a UIImageView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你丑哭了我 2024-09-01 04:35:49

创建一个黑色背景的 UIView,高 1 像素,宽 320 像素。

Create a UIView with a black background that is 1 pixel high and 320 pixels wide.

征棹 2024-09-01 04:35:49

使用 UIView:

UIView * separator = [[UIView alloc] initWithFrame:CGRectMake(x, y, 320, 1)];
separator.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
[self.view addSubview:separator];
[separator release];

Use a UIView:

UIView * separator = [[UIView alloc] initWithFrame:CGRectMake(x, y, 320, 1)];
separator.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
[self.view addSubview:separator];
[separator release];
雾里花 2024-09-01 04:35:49

虽然 Jasarien 的解决方案很好且简单,但它并没有创建实际的 1 像素细线,而是在 2X 设备上创建 2 像素宽的线。

我找到了一篇关于如何创建的博客文章真正的1像素细发际线。
我们需要一个实用的 UIView 子类。对于斯威夫特来说,这将是:

import UIKit

class HairlineView: UIView {
    override func awakeFromNib() {
        guard let backgroundColor = self.backgroundColor?.CGColor else { return }
        self.layer.borderColor = backgroundColor
        self.layer.borderWidth = (1.0 / UIScreen.mainScreen().scale) / 2;
        self.backgroundColor = UIColor.clearColor()
    }
}

While Jasarien's solution is nice and simple, it doesn't create an actual 1 pixel hairline, but a 2 pixel wide line on 2X devices.

I found a blog post on how to create the real 1 pixel thin hairline.
We need a utility UIView subclass. For Swift that would be:

import UIKit

class HairlineView: UIView {
    override func awakeFromNib() {
        guard let backgroundColor = self.backgroundColor?.CGColor else { return }
        self.layer.borderColor = backgroundColor
        self.layer.borderWidth = (1.0 / UIScreen.mainScreen().scale) / 2;
        self.backgroundColor = UIColor.clearColor()
    }
}
稀香 2024-09-01 04:35:49

对于水平线

UIView *horizontalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,1,linelenth)];
horizontalLine.backgroundColor = [UIColor blackColor];
[self. view addSubView:horizontalLine];
[horizontalLine release];

对于垂直线

UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,linelenth,1)];
verticalLine.backgroundColor = [UIColor blackColor];
[self. view addSubView:verticalLine];
[verticalLine release];

For Horizontal Line

UIView *horizontalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,1,linelenth)];
horizontalLine.backgroundColor = [UIColor blackColor];
[self. view addSubView:horizontalLine];
[horizontalLine release];

For Vertical Line

UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,linelenth,1)];
verticalLine.backgroundColor = [UIColor blackColor];
[self. view addSubView:verticalLine];
[verticalLine release];
时光匆匆的小流年 2024-09-01 04:35:49

试试这个

extension CALayer {

    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {

        let border = CALayer()

        switch edge {
        case .top:
            border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness)
        case .bottom:
            border.frame = CGRect(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
        case .left:
            border.frame = CGRect(x: 0, y: 0, width: thickness, height: frame.height)
        case .right:
            border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
        default:
            break
        }

        border.backgroundColor = color.cgColor;

        addSublayer(border)
    }

Try this

extension CALayer {

    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {

        let border = CALayer()

        switch edge {
        case .top:
            border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness)
        case .bottom:
            border.frame = CGRect(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
        case .left:
            border.frame = CGRect(x: 0, y: 0, width: thickness, height: frame.height)
        case .right:
            border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
        default:
            break
        }

        border.backgroundColor = color.cgColor;

        addSublayer(border)
    }
横笛休吹塞上声 2024-09-01 04:35:49

要从头开始快速创建一条线,只需执行以下操作即可

let line = UIView()
view.addSubview(line)
line.translatesAutoresizingMaskIntoConstraints = false
line.widthAnchor.constraint(equalToConstant: view.bounds.width - 40).isActive = true
line.heightAnchor.constraint(equalToConstant: 1).isActive = true
line.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
line.topAnchor.constraint(equalTo: userName.bottomAnchor,constant: 20).isActive = true
line.backgroundColor = .gray

线的宽度等于设备宽度 - 一些常量,并且从左侧添加相同的常量,因此我们得到一条类似这样的线


To create a line in swift from scratch, just do this

let line = UIView()
view.addSubview(line)
line.translatesAutoresizingMaskIntoConstraints = false
line.widthAnchor.constraint(equalToConstant: view.bounds.width - 40).isActive = true
line.heightAnchor.constraint(equalToConstant: 1).isActive = true
line.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
line.topAnchor.constraint(equalTo: userName.bottomAnchor,constant: 20).isActive = true
line.backgroundColor = .gray

The line's width is equal to the device width - some constant and that same constant is added from left so we get a line something like this


春夜浅 2024-09-01 04:35:49

您可以将其放入 UIView 中

- (void)drawRect:(CGRect)rect
{

//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();

//// Shadow Declarations
CGColorRef outerShadow = [UIColor blackColor].CGColor;
CGSize outerShadowOffset = CGSizeMake(0, 1);
CGFloat outerShadowBlurRadius = 2;

//// Abstracted Graphic Attributes
CGRect rectangleFrame = CGRectMake(0, 0, self.frame.size.width, 3);


//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: rectangleFrame];
[[UIColor lightGrayColor] setFill];
[rectanglePath fill];

////// Rectangle Inner Shadow
CGRect rectangleBorderRect = CGRectInset([rectanglePath bounds], -outerShadowBlurRadius, -outerShadowBlurRadius);
rectangleBorderRect = CGRectOffset(rectangleBorderRect, -outerShadowOffset.width, -outerShadowOffset.height);
rectangleBorderRect = CGRectInset(CGRectUnion(rectangleBorderRect, [rectanglePath bounds]), -1, -1);

UIBezierPath* rectangleNegativePath = [UIBezierPath bezierPathWithRect: rectangleBorderRect];
[rectangleNegativePath appendPath: rectanglePath];
rectangleNegativePath.usesEvenOddFillRule = YES;

CGContextSaveGState(context);
{
    CGFloat xOffset = outerShadowOffset.width + round(rectangleBorderRect.size.width);
    CGFloat yOffset = outerShadowOffset.height;
    CGContextSetShadowWithColor(context,
                                CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
                                outerShadowBlurRadius,
                                outerShadow);

    [rectanglePath addClip];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rectangleBorderRect.size.width), 0);
    [rectangleNegativePath applyTransform: transform];
    [[UIColor grayColor] setFill];
    [rectangleNegativePath fill];
}
CGContextRestoreGState(context);
}

You can drop this into a UIView

- (void)drawRect:(CGRect)rect
{

//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();

//// Shadow Declarations
CGColorRef outerShadow = [UIColor blackColor].CGColor;
CGSize outerShadowOffset = CGSizeMake(0, 1);
CGFloat outerShadowBlurRadius = 2;

//// Abstracted Graphic Attributes
CGRect rectangleFrame = CGRectMake(0, 0, self.frame.size.width, 3);


//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: rectangleFrame];
[[UIColor lightGrayColor] setFill];
[rectanglePath fill];

////// Rectangle Inner Shadow
CGRect rectangleBorderRect = CGRectInset([rectanglePath bounds], -outerShadowBlurRadius, -outerShadowBlurRadius);
rectangleBorderRect = CGRectOffset(rectangleBorderRect, -outerShadowOffset.width, -outerShadowOffset.height);
rectangleBorderRect = CGRectInset(CGRectUnion(rectangleBorderRect, [rectanglePath bounds]), -1, -1);

UIBezierPath* rectangleNegativePath = [UIBezierPath bezierPathWithRect: rectangleBorderRect];
[rectangleNegativePath appendPath: rectanglePath];
rectangleNegativePath.usesEvenOddFillRule = YES;

CGContextSaveGState(context);
{
    CGFloat xOffset = outerShadowOffset.width + round(rectangleBorderRect.size.width);
    CGFloat yOffset = outerShadowOffset.height;
    CGContextSetShadowWithColor(context,
                                CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
                                outerShadowBlurRadius,
                                outerShadow);

    [rectanglePath addClip];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rectangleBorderRect.size.width), 0);
    [rectangleNegativePath applyTransform: transform];
    [[UIColor grayColor] setFill];
    [rectangleNegativePath fill];
}
CGContextRestoreGState(context);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文