我刚开始尝试将我的Android应用转换为IOS,并想知道如何转换我的Android XML drawables。目标是能够使用具有x和y位置的形状创建纹理,以便不拉伸纹理。 IOS中的Android drawables有类似的东西吗?如果不是,我将如何使用从IOS中的drawable创建的纹理?例如,如何将drawable转换为下面的文本框?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="10dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape
android:shape="rectangle">
<size
android:height="13dp"
android:width="46dp"
/>
<solid
android:color="@color/grey"
/>
</shape>
</item>
<item android:top="0dp" android:left="0dp" android:bottom="10dp" android:right="0dp">
<shape
android:shape="rectangle">
<size
android:height="13dp"
android:width="46dp"
/>
<solid
android:color="@color/very_light_grey"
/>
</shape>
</item>
<item android:top="0dp" android:left="2dp" android:bottom="0dp" android:right="2dp">
<shape
android:shape="rectangle">
<size
android:height="13dp"
android:width="46dp"
/>
<solid
android:color="@color/grey"
/>
</shape>
</item>
<item android:top="1dp" android:left="3dp" android:bottom="3dp" android:right="3dp">
<shape
android:shape="rectangle">
<size
android:height="20dp"
android:width="40dp"
/>
<solid
android:color="@color/white"
/>
</shape>
</item>
</layer-list>
答案 0 :(得分:0)
UIBezierPath是我能够找到的最接近Android Drawables的东西。虽然它不像Android Drawables那样使用XML,但它的功能与Android Drawables非常相似,您可以使用基本形状为UI组件创建纹理。问题中Android Drawable的转换如下所示:
import Foundation
import UIKit
class CustomUIInputText : UITextField{
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func draw(_ rect: CGRect) {
let darkGrey = ColorUtilities.hexStringToUIColor(hex: "808080")
let lightGrey = ColorUtilities.hexStringToUIColor(hex: "efefef")
let lightGreyRect = UIBezierPath(rect: rect)
let width = rect.size.width
let height = rect.size.height
let yValue = rect.origin.y
let xValue = rect.origin.x
lightGrey.setFill()
lightGreyRect.fill()
let darkGreyRect = UIBezierPath(rect: CGRect(x: xValue, y:yValue + ((2 * height)/3), width: width, height: height/3))
darkGrey.setFill()
darkGreyRect.fill()
let darkGreyRect2 = UIBezierPath(rect: CGRect(x: xValue+3, y:yValue, width: width - 6, height: height))
darkGrey.setFill()
darkGreyRect2.fill()
let whiteRect = UIBezierPath(rect: CGRect(x: xValue + 4, y:yValue + 1, width: width - 8, height: height - 4))
UIColor.white.setFill()
whiteRect.fill()
}
}