Chat app (1)
파이널 프로젝트에서 우리가 사용할 필요 기술이 하나 있어서 준비할겸 적어본다.
초반에는 딱히 서술할게 없을듯…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
extension UIButton {
func attributedText(firstString: String, secondString: String) {
let atts: [NSAttributedString.Key: Any] = [.foregroundColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).withAlphaComponent(0.7), .font: UIFont.systemFont(ofSize: 16)]
let attributedTitle = NSMutableAttributedString(string: "\(firstString) ", attributes: atts)
let secondAtts: [NSAttributedString.Key: Any] = [.foregroundColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).withAlphaComponent(0.88), .font: UIFont.boldSystemFont(ofSize: 16)]
attributedTitle.append(NSAttributedString(string: secondString, attributes: secondAtts))
setAttributedTitle(attributedTitle, for: .normal)
}
}
private lazy var forgetPasswordButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Forget your password? Get Help Signing in", for: .normal)
button.tintColor = .black
button.setHeight(50)
button.titleLabel?.font = .boldSystemFont(ofSize: 19)
button.addTarget(self, action: #selector(handleForgetPassword), for: .touchUpInside)
return button
}()
private lazy var forgetPasswordButton: UIButton = {
let button = UIButton(type: .system)
button.attributedText(firstString: "Forget your password?", secondString: "Get Help Signing in")
button.tintColor = .black
button.setHeight(50)
button.titleLabel?.font = .boldSystemFont(ofSize: 19)
button.addTarget(self, action: #selector(handleForgetPassword), for: .touchUpInside)
return button
}()
이렇게 함으로써 글자마다 다르게 표현이 가능해진다.
LoginVM 만들기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct LoginViewModel {
var email: String?
var password: String?
var formIsFailed: Bool {
return email?.isEmpty == false && password?.isEmpty == false
}
var backgroundColor: UIColor {
return formIsFailed ? (UIColor.black) : (UIColor.black.withAlphaComponent(0.5))
}
var buttonTitleColor: UIColor {
return formIsFailed ? (UIColor.white) : (UIColor(white: 1, alpha: 0.7))
}
}
@objc func handleTextChanged(sender: UITextField) {
sender == emailTF ? (viewModel.email = sender.text) : (viewModel.password = sender.text)
updateForm()
}
private func updateForm() {
loginButton.isEnabled = viewModel.formIsFailed
loginButton.backgroundColor = viewModel.backgroundColor
loginButton.setTitleColor(viewModel.buttonTitleColor, for: .normal)
}
TextField Customizing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class CustomTextField: UITextField {
init(placeholder: String, keyboardType: UIKeyboardType = .default, isSecure: Bool = false) {
super.init(frame: .zero)
let spacer = UIView()
spacer.setDimensions(height: 50, width: 12)
leftView = spacer
leftViewMode = .always
borderStyle = .none
textColor = .black
keyboardAppearance = .light
clearButtonMode = .whileEditing
backgroundColor = .systemGray6
setHeight(50)
self.keyboardType = keyboardType
isSecureTextEntry = isSecure
attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [.foregroundColor: UIColor.black.withAlphaComponent(0.7)])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
ImageView Customizing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class CustomImageView: UIImageView {
init(image: UIImage? = nil, width: CGFloat? = nil, height: CGFloat? = nil, cornerRadius: CGFloat = 0) {
super.init(frame: .zero)
contentMode = .scaleAspectFit
layer.cornerRadius = cornerRadius
if let image = image {
self.image = image
}
if let width = width {
setWidth(width)
}
if let height = height {
setHeight(height)
}
if let backgroundColor = backgroundColor {
self.backgroundColor = backgroundColor
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Label Customizing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class CustomLabel: UILabel {
init(text: String, textFont: UIFont = .systemFont(ofSize: 14), labelColor: UIColor = .black) {
super.init(frame: .zero)
self.text = text
font = textFont
textColor = labelColor
textAlignment = .center
numberOfLines = 0
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
강의 초반이라 딱히 서술할게 없다…
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.