Issue
I just began trying to convert my Android app to IOS and was wondering how I can convert my Android XML drawables. The goal is to be able to create textures using shapes with x and y positions so that the texture is not stretched. Is there a similar thing to Android drawables in IOS? If not how would I use the textures created from the drawables in IOS? For example how would I convert the drawable for a textbox below?
<?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>
Solution
UIBezierPath is the closest thing to Android Drawables that I was able to find. Although it is not in XML like Android Drawables, the functionality is very similar to Android Drawables where you can use basic shapes to create textures for UI components.The conversion of the Android Drawable in the question is displayed below:
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()
}
}
Answered By - KevinZ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.