文本框文字怎麼垂直居中(符號在文本框內垂直居中顯示)
2023-10-13 13:17:20
一般情況下,如果不對*號做任何處理,在展示的時候,是偏向上展示的,如下圖所示:
為了實現***和其他文本內容垂直方向居中顯示,可以使用富文本處理一下,處理後的效果如下圖所示:
guard value.contains("***") == false else { let range = NSRange(location: 0, length: 3) digitalAttributes = [.font: UIFont.semiboldOfSize(15), .baselineOffset: -4] as [NSAttributedString.Key : Any] targetAttribute.addAttributes(digitalAttributes, range: range) return targetAttribute}
其中.baselineOffset是NSAttributedString的屬性,通常叫它基礎偏移量,就是通過設置.baselineOffset來實現***垂直方向居中顯示的
key:.baselineOffset
value:包含浮點數的NSNumber對象
基礎偏移量:正值向上偏移,負值向下偏移,默認0(不偏移)
,