설계 01 · 드로잉
본체와 꼬리를 다른 기법으로 그린다
말풍선은 본체(둥근 사각형)와 꼬리(삼각형)의 테두리가 한 획처럼 이어져 보여야 합니다. 점선 스타일까지 지원하려면 layer 테두리만으로는 부족합니다.
드로잉을 부위별로 분리했습니다. 본체는 CAShapeLayer(둥근 사각 + 점선 대시), 꼬리는 draw(_:)의 UIBezierPath 삼각형. 두 뷰를 borderWidth만큼 겹치는 제약으로 합성해 테두리가 이어집니다.
꼬리는 모서리 길이 × positionRatio로 기준점을 잡고 arrowSize만큼 벌린 3점을 방향별로 계산합니다. 4방향(top/bottom/left/right) 어디든 임의 비율 위치에 붙습니다.
// 모서리 길이 x 비율로 기준점 case .top: pointTip = CGPoint(x: position, y: rect.height - arrowSize) pointLeft = CGPoint(x: position - arrowSize, y: rect.height) pointRight = CGPoint(x: position + arrowSize, y: rect.height) let path = UIBezierPath() path.move(to: arrowPoints.left) path.addLine(to: arrowPoints.tip) path.addLine(to: arrowPoints.right) if style.contains(.fill) { filledColor.setFill(); path.fill() }