# UISemanticContentAttribute: 如何將 UIButton 的圖片移至右邊 ## UIkit 的Button 預設是圖片在左邊 由於一些畫面需要,可能需要調整圖片的位置。可以透過一些設定圖片邊件數值得程式碼,但是也可以透過設定一些更簡單的參數調整。 ![image.png](https://hackmd.io/_uploads/HyPnlarmT.png) # UISemanticContentAttribute 語意內容屬性 UISemanticContentAttribute(語意內容屬性)是視圖內容的語意描述,用於確定在從左到右和從右到左的佈局之間切換時,視圖是否應該翻轉。 這個屬性用於適應不同語言和文字方向的介面,以確保內容的呈現在不同的在地化環境中是正確的。 透過這個屬性,iOS應用程式可以根據使用者的語言和本地化設定自動調整介面的方向,以確保文字和視圖的佈局是合適的。 這有助於確保應用程式在各種語言和文字方向下都能提供一致的使用者體驗 ```swift= public enum UISemanticContentAttribute : Int, @unchecked Sendable { case unspecified = 0 case playback = 1 case spatial = 2 case forceLeftToRight = 3 case forceRightToLeft = 4 } ``` unspecified 默認值,非特定的,當未明確指定語義屬性時,系統將根據應用程式的本地化設定和使用者首選項來決定視圖的方向。 playback 音樂播放器的分類。 spatial 地圖或是圖形。 forceLeftToRight 強制從左至右。 forceRightToLeft 強制從右至左。 # forceLeftToRight 這邊使用從右至左的屬性,效果如下。 ```swift= button.semanticContentAttribute = .forceRightToLeft ``` ![image.png](https://hackmd.io/_uploads/By5ARaS76.png) 完整的程式碼: ```swift= let loginButton: UIButton = { let button = UIButton(configuration: .gray()) button.setTitle("Login", for: .normal) button.configuration?.cornerStyle = .capsule button.configuration?.baseBackgroundColor = .lightGray button.setImage(UIImage(systemName: "globe"), for: .normal) button.semanticContentAttribute = .forceRightToLeft button.translatesAutoresizingMaskIntoConstraints = false return button }()