Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Created December 12, 2021 06:46
Show Gist options
  • Save trilliwon/59dafd242943c4d135f7c72a74f47370 to your computer and use it in GitHub Desktop.
Save trilliwon/59dafd242943c4d135f7c72a74f47370 to your computer and use it in GitHub Desktop.

Declare UITextField and setup fields. Set decorated NSAttributedString for UITextField.attributedPlaceholder to set custom color or font etc.

drawing

let textField: UITextField = {
		$0.returnKeyType = .done
		$0.attributedPlaceholder = NSAttributedString(
				string: "이메일을 입력하세요",
				attributes: [NSAttributedString.Key.foregroundColor: UIColor.systemRed.withAlphaComponent(0.7)]
		)
		$0.textAlignment = .left
		$0.font = UIFont.systemFont(ofSize: 30, weight: .bold)
		$0.tintColor = UIColor.systemGreen
		$0.setContentHuggingPriority(.required, for: .horizontal)
		return $0
}(UITextField())

Drop shadow for only text use NSShadow()

drawing

textField.addTarget(self, action: #selector(didChangeText(_:)), for: .editingChanged)

@objc
func didChangeText(_ textField: UITextField) {
		guard let text = textField.text else { return }
		textField.attributedText = shadowedAttributedText(from: text)
}

func shadowedAttributedText(from string: String) -> NSAttributedString {
		NSAttributedString(string: string, attributes: [
				NSAttributedString.Key.font: UIFont.systemFont(ofSize: 30, weight: .bold),
				NSAttributedString.Key.foregroundColor: UIColor.green,
				NSAttributedString.Key.shadow: {
						$0.shadowColor = UIColor.black.withAlphaComponent(0.5)
						$0.shadowOffset = CGSize.zero
						$0.shadowBlurRadius = 4
						return $0
				}(NSShadow())
		])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment