代码之家  ›  专栏  ›  技术社区  ›  4thSpace wkw

创建水平线

  •  37
  • 4thSpace wkw  · 技术社区  · 14 年前

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

    5 回复  |  直到 7 年前
        1
  •  82
  •   Jasarien    7 年前

    创建一个具有1像素高、320像素宽的黑色背景的uiview。

        2
  •  18
  •   Tom Irving    14 年前

    使用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];
    
        3
  •  11
  •   Phantrast    9 年前

    虽然Jasarien的解决方案很好也很简单,但它并没有创建 实际1像素发际线 ,但在2x设备上有一条2像素宽的线。

    我找到了一个 blog post 关于如何创建真正的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()
        }
    }
    
        4
  •  5
  •   Alex Nolasco    8 年前

    对于水平线

    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];
    
        5
  •  -1
  •   Critter    12 年前

    你可以把这个放到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);
    }