我想在一个UITextView
周围加上细灰色的边框。我已经查阅了苹果的文档,但未能找到任何属性。请帮忙。
只是一个小的补充。如果你把边框加宽一点,它会干扰到文本的左右两侧。为了避免这种情况,我添加了以下行:
self.someTextView.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0);
class BorderedTextView: UITextView {
let textField = UITextField()
required init?(coder: NSCoder) {
super.init(coder: coder)
insertTextField()
}
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
insertTextField()
}
convenience init() {
self.init(frame: .zero, textContainer: nil)
}
private func insertTextField() {
delegate = self
textField.borderStyle = .roundedRect
insertSubview(textField, at: 0)
}
override func layoutSubviews() {
super.layoutSubviews()
textField.frame = bounds
}
}
extension BorderedTextView: UITextViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
textField.frame = bounds
}
}
borderStyle
属性使其正常工作(除了按照这里的答案)。#import <QuartzCore/QuartzCore.h>
..
phoneTextField.layer.borderWidth = 1.0f;
phoneTextField.layer.borderColor = [[UIColor blueColor] CGColor];
phoneTextField.borderStyle = UITextBorderStyleNone;
myText.layer.borderColor = UIColor.lightGray.cgColor
myText.layer.borderWidth = 1.0
UIButton
并将UITextView
的背景颜色设置为clearColor来解决了这个问题。 这样可以在不需要额外的代码或包的情况下完成工作。