1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Tips] UITextviewをUILabelっぽく使う

Last updated at Posted at 2018-12-29

Link実装したいけど、UILabelに付加するのは難しいし…」みたいなことがあったので。

スクリーンショット 2018-12-29 15.23.02.png

概要

UITextviewをUILabelっぽく使うためのTips。なお、IBDesignable入門からの抜き出し情報。

やること

  1. IBDesignable対応クラスを用意。
  • 使いたいUITextView継承クラスに@IBDesignbableを付加。
  • 使いたいUITextView継承クラスが特に無い場合は、UITextViewのIBDesignable用クラスを作る。
  1. UITextViewのエクステンションを作成し、必要なプロパティを追加。
  • Padding調整プロパティ
  • 行数指定プロパティ
  1. IBファイル(StoryboardやXib)でUITextViewを配置し、1で作成したクラスをCustomClassに設定。
  2. Paddingを0に指定。
  3. Linesを指定。
  4. 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 }
    }
}

備考

(とくになし)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?