本日は9/9 は台風の影響のため、電車が運転見合わせ状態から午前中に運転再開すると、とても混み合うので午前休にしました。
満員電車に乗るのも、危険だと思うので、安全第一です。
この間にブログでも記載しようと思います。
以前、業務でラベルのタグ付けをデザインする上で、UILabelのデザイン拡張をするプログラムを作成しました。
今回は題名のAllLabelについて、
こちらの機能はStoryboardでラベルの文字位置を可変、ラベルのデザインを可変できる機能です。
デフォルトでは文字位置を可変するプログラムはないので、実施する場合は自装する必要があります。
プログラムの紹介
自動的に高さを決めるプログラム、 UIEdgeInsets.zeroの場合、すなわち値が0の場合は自動的に余白を設定します。初めにStoryboardで設定した値です。
var textInsets = UIEdgeInsets.zero { didSet { invalidateIntrinsicContentSize() } }
この記述がされている場合は、Storybordのデザインを反映する機能です。
@IBDesignable
この記述がされている場合は、Storybord右のアトリビュートインスペクターに反映する機能です。
@IBInspectable
@IBInspectable var cornerRadius: CGFloat = 0.0 @IBInspectable var borderColor: UIColor = UIColor.clear @IBInspectable var borderWidth: CGFloat = 0.0
cornerRadiusやborderColorなどの値をStoryboard上のエディターで可変出来ます。
オープンソースライブラリーは軽いものでも、何MBの容量がありますが、自作する場合は何KB容量で運用できます。
複雑なUIなども自作出来るとアプリ自体のパフォーマンスが向上すると思います。工数もあるので、判断基準は千差万別です。
overrideと記載されているメソッドは基本的にAppleのデフォルトメソッドです。
カーソルを合わして、command + track padをエンターで画像のような画面になり、実装してある元のクラスにjump出来ます。
実装してある元のクラスの解説になります。このクラスを使用する場合にoverrideを記述します。
extensionは拡張できる機能です。
getでプロパティ値を取得して、setのnewValueで、新しい値になります。
public extension Int からのメソッドは表示形式です。
数字のクラスの中に文字メソッドを作成して、数字を指定した文字形式に変換するクラスです。
使い方
formattedJPString.text = 123456789.formattedJPString jpyLabel.text = 123456789.JPYString 結果 123,456,789 結果 ¥123,456,789
制約が更新されるとlayoutSubviews()
が呼び出され、フレームが更新されるので、プログラムで変更する場合に使用します。
override func layoutSubviews()
今回の場合に
usdLabel.priceCount = 5434 とすると画像のようになります。
インテントを設定するメソッドです。このメソッドで文字のインテントを実現しています。
override func drawText(in rect: CGRect)
このクラスでは、invalidateIntrinsicContentSizeで自動計算した値を、インテントを調整できるCGRectを作成して設定しています。起動時に処理され、インテントを設定した値を反映します。後からでも呼び出すことは出来ますが、使い方としては、Storyboard上で設定した値を反映する形になります。
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect
今回追加した機能、文字に対する影。(UILabelではLabel自体に影はつきません。)
使い方
jpyLabel.textShadow(shadowBlurRadius: 5.0, shadowColor: .green, shadowOffset: CGSize(width: 25.0, height: jpyLabel.frame.height/4), textColor: .black)
結果
以上、UILabelの拡張クラス AllLabelの紹介です。貴重なお時間お読みくださいまして、ありがとうございます。
11 Responses
It’s the best time to make a few plans for the long run and it’s time to be
happy. I have read this publish and if I could I want to counsel you few fascinating things or suggestions.
Perhaps you could write subsequent articles relating to this
article. I desire to learn even more things approximately it!
Howdy would you mind letting me know which webhost you’re using?
I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot faster then most.
Can you recommend a good internet hosting provider at a
fair price? Thank you, I appreciate it! Hi, I do think this is an excellent website.
I stumbledupon it 😉 I will return yet again since I book
marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.
http://nissan.com
Thanks for your comment!
The following webhost is used.
https://www.sakura.ad.jp
I participate in the app project (2) and also develop and research private apps.
Also thank you. Good luck
wonderful submit, very informative. I wonder why the opposite specialists of this sector do not understand this.
You should continue your writing. I am confident, you’ve a huge readers’ base already!
Thank you for the good writeup. It in fact was a amusement account it. https://www.granitelacroix.com
Thanks for the auspicious writeup. It in fact used to be a amusement account it.
Glance advanced to more brought agreeable from
you! However, how could we communicate?
I’d constantly want to be update on new articles on this website,
saved to favorites!
Thank you
What?s up, I read your new stuff on a regular basis. Your story-telling style is awesome,
keep doing what you?re doing! https://nanoprotection.ca
Nice blog here! Also your site lots up very fast! What web host are you
using? Can I am getting your associate link for your host?
I want my web site loaded up as fast as yours lol
I am sure this paragraph has touched all the
internet visitors, its really really pleasant article
on building up new web
site https://checkersmoda.com
It?s the best time to
make a few plans for the longer term and it?s time to
be happy. http://www.octeaujoaillier.com