[Tips] UITextview를 UILabel처럼 사용

「Link 구현하고 싶지만, UILabel에 부가하는 것은 어렵고… 」 같은 것이 있었기 때문에.



개요



UITextview를 UILabel처럼 사용하기위한 Tips. 덧붙여 IBDesignable 입문 로부터의 추출 정보.

할 일


  • IBDesignable 대응 클래스를 준비.
  • 사용하고 싶은 UITextView 상속 클래스에 @IBDesignbable 를 부가.
  • 사용하고 싶은 UITextView 상속 클래스가 특별히 없는 경우는, UITextView 의 IDesignable용 클래스를 만든다.

  • UITextView의 확장을 만들고 필요한 속성을 추가합니다.
  • Padding 조정 속성
  • 행 수 지정 속성

  • IB 파일(Storyboard 또는 Xib)에 UITextView를 배치하고 1로 만든 클래스를 CustomClass로 설정합니다.
  • Padding를 0으로 지정.
  • Lines를 지정.
  • Scrolling Enabled를 false로 지정.

  • 코드



    IBDesinable 용 UITextView 상속 클래스
    @available(*, unavailable, message: "Only use it at Storybord or Xib. When referring it from their file to Swift file, replace reference class name to inherited class.", renamed: "UITextView")
    @IBDesignable final class DesignableTextView: UITextView {
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
        override init(frame: CGRect, textContainer: NSTextContainer?) {
            super.init(frame: frame, textContainer: textContainer)
        }
    }
    

    UITextViewExtension
    extension UITextView {
        @available(*, unavailable, message: "Only use it at Storybord or Xib. When referring it from their file to Swift file, replace reference class name to inherited class.", renamed: "textContainerInset.top")
        @IBInspectable var topPadding: Float {
            get { return Float(self.textContainerInset.top) }
            set { textContainerInset.top = CGFloat(newValue) }
        }
        @available(*, unavailable, message: "Only use it at Storybord or Xib. When referring it from their file to Swift file, replace reference class name to inherited class.", renamed: "textContainerInset.left")
        @IBInspectable var leftPadding: Float {
            get { return Float(self.textContainerInset.left + 5) }
            set { textContainerInset.left = CGFloat(newValue - 5) }
        }
        @available(*, unavailable, message: "Only use it at Storybord or Xib. When referring it from their file to Swift file, replace reference class name to inherited class.", renamed: "textContainerInset.right")
        @IBInspectable var rightPadding: Float {
            get { return Float(self.textContainerInset.right + 5) }
            set { textContainerInset.right = CGFloat(newValue - 5) }
        }
        @available(*, unavailable, message: "Only use it at Storybord or Xib. When referring it from their file to Swift file, replace reference class name to inherited class.", renamed: "textContainerInset.bottom")
        @IBInspectable var bottomPadding: Float {
            get { return -1 * Float(self.textContainerInset.bottom) }
            set { textContainerInset.bottom = -1 * CGFloat(newValue) }
        }
        @available(*, unavailable, message: "Only use it at Storybord or Xib. When referring it from their file to Swift file, replace reference class name to inherited class.", renamed: "textContainer.maximumNumberOfLines")
        @IBInspectable var lines: Int {
            get { return textContainer.maximumNumberOfLines }
            set { textContainer.maximumNumberOfLines = newValue }
        }
    }
    

    비고



    (특히 없음)

    좋은 웹페이지 즐겨찾기