有边框的 UITextView

发布于 2024-08-29 05:04:44 字数 71 浏览 15 评论 0原文

我想在 UITextView 周围有一个灰色的细边框。我已经浏览了苹果文档,但找不到任何属性。请帮忙。

I want to have a thin gray border around a UITextView. I have gone through the Apple documentation but couldn't find any property there. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

勿忘初心 2024-09-05 05:04:45

我添加 UIImageView 作为 UITextView 的子视图。这与 UITextField 上的原生边框相匹配,包括从上到下的渐变:

在此处输入图像描述

textView.backgroundColor = [UIColor clearColor];
UIImageView *borderView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *textFieldImage = [[UIImage imageNamed:@"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];
borderView.image = textFieldImage;
[textField addSubview: borderView];
[textField sendSubviewToBack: borderView];

这些是我使用的 png 图像和 jpg 表示形式:

@1x

@2x

在此处输入图像描述

I add UIImageView as a subview of the UITextView. This matches the native border on a UITextField, including the gradient from top to bottom:

enter image description here

textView.backgroundColor = [UIColor clearColor];
UIImageView *borderView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *textFieldImage = [[UIImage imageNamed:@"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];
borderView.image = textFieldImage;
[textField addSubview: borderView];
[textField sendSubviewToBack: borderView];

These are the png images I use, and a jpg representation:

@1x

@2x

enter image description here

秋凉 2024-09-05 05:04:45

效果很好,但颜色应该是 CGColor,而不是 UIColor

view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];

Works great, but the color should be a CGColor, not UIColor:

view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];
春庭雪 2024-09-05 05:04:45

我相信以上答案适用于 Swift 的早期版本。我在 Google 上搜索了一下,下面的代码适用于 Swift 4。只是将其分享给可能受益的人。

self.textViewName.layer.borderColor = UIColor.lightGray.cgColor
self.textViewName.layer.borderWidth = 1.0
self.textViewName.layer.cornerRadius = 8

快乐编码!

I believe the above answers are for the previous versions of Swift. I Googled a bit and the below code works for Swift 4. Just sharing it for whoever it may benefit.

self.textViewName.layer.borderColor = UIColor.lightGray.cgColor
self.textViewName.layer.borderWidth = 1.0
self.textViewName.layer.cornerRadius = 8

Happy Coding!

怪我鬧 2024-09-05 05:04:45

对于 Swift 编程,请使用这个

tv_comment.layer.borderWidth = 2
tv_comment.layer.borderColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1).CGColor

for Swift Programming, use this

tv_comment.layer.borderWidth = 2
tv_comment.layer.borderColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1).CGColor
会发光的星星闪亮亮i 2024-09-05 05:04:45

这与原始 UITextField 尽可能接近

func updateBodyTextViewUI() {
    let borderColor = UIColor.init(red: 212/255, green: 212/255, blue: 212/255, alpha: 0.5)

    self.bodyTextView.layer.borderColor = borderColor.CGColor
    self.bodyTextView.layer.borderWidth = 0.8
    self.bodyTextView.layer.cornerRadius = 5
}

this is as close as I could from an original UITextField

func updateBodyTextViewUI() {
    let borderColor = UIColor.init(red: 212/255, green: 212/255, blue: 212/255, alpha: 0.5)

    self.bodyTextView.layer.borderColor = borderColor.CGColor
    self.bodyTextView.layer.borderWidth = 0.8
    self.bodyTextView.layer.cornerRadius = 5
}
我是男神闪亮亮 2024-09-05 05:04:45

您可以从故事板 - 身份检查器 - 用户定义的运行时属性

在此处输入图像描述

you can add border to UITextView from the Storyboard - Identity Inspector - User Defined Runtime Attribute

enter image description here

花开柳相依 2024-09-05 05:04:45

从 iOS 8 和 Xcode 6 开始,我现在发现最好的解决方案是子类化 UITextView 并将子类标记为 IB_DESIGNABLE,这将允许您在 Storyboard 中查看边框。

标题:

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface BorderTextView : UITextView

@end

实现:

#import "BorderTextView.h"

@implementation BorderTextView

- (void)drawRect:(CGRect)rect
{
    self.layer.borderWidth = 1.0;
    self.layer.borderColor = [UIColor blackColor].CGColor;
    self.layer.cornerRadius = 5.0f;
}

@end

然后只需在故事板中拖出 UITextView 并将其类设置为 BorderTextView

As of iOS 8 and Xcode 6, I now find the best solution is to subclass UITextView and mark the subclass as an IB_DESIGNABLE, which will allow you to view the border in storyboard.

Header:

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface BorderTextView : UITextView

@end

Implementation:

#import "BorderTextView.h"

@implementation BorderTextView

- (void)drawRect:(CGRect)rect
{
    self.layer.borderWidth = 1.0;
    self.layer.borderColor = [UIColor blackColor].CGColor;
    self.layer.cornerRadius = 5.0f;
}

@end

Then just drag out your UITextView in storyboard and set its class to BorderTextView

乜一 2024-09-05 05:04:45

使它起作用的事情(除了遵循此处的答案之外)是添加 borderStyle 属性:

#import <QuartzCore/QuartzCore.h>
..

phoneTextField.layer.borderWidth = 1.0f;
phoneTextField.layer.borderColor = [[UIColor blueColor] CGColor];
phoneTextField.borderStyle = UITextBorderStyleNone;

The thing that made it work (in addition to following the answers here) is adding the borderStyle attribute:

#import <QuartzCore/QuartzCore.h>
..

phoneTextField.layer.borderWidth = 1.0f;
phoneTextField.layer.borderColor = [[UIColor blueColor] CGColor];
phoneTextField.borderStyle = UITextBorderStyleNone;
飘逸的'云 2024-09-05 05:04:45

只是一个小小的补充。如果将边框设置得更宽一些,则会干扰文本的左侧和右侧。为了避免这种情况,我添加了以下行:

self.someTextView.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0);

Just a small addition. If you make the border a bit wider, it will interfere with the left and right side of text. To avoid that, I added the following line:

self.someTextView.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0);
倾其所爱 2024-09-05 05:04:45

在 Swift 3 中,您可以使用以下两行:

myText.layer.borderColor = UIColor.lightGray.cgColor

myText.layer.borderWidth = 1.0

In Swift 3, you may use the following two lines:

myText.layer.borderColor = UIColor.lightGray.cgColor

myText.layer.borderWidth = 1.0
疑心病 2024-09-05 05:04:45

一个优雅的解决方案是在底部插入一个真正的 UITextField 并防止它随内容滚动。这样你就可以拥有正确的深色模式边框。

An elegant solution would be to insert a real UITextField on the bottom and prevent it to scroll with the content. This way you have even the correct dark mode borders. ????

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
  }
}
随风而去 2024-09-05 05:04:45

我通过在 UITextView 后面放置一个完全禁用的 UIButton 并将 UITextView 的背景颜色设置为clearColor,在情节提要中解决了这个问题。这不需要额外的代码或包就可以工作。

I solved this problem in storyboard by putting a fully disabled UIButton behind the UITextView and making the background color of the UITextView clearColor. This works without requiring extra code or packages.

热鲨 2024-09-05 05:04:44
#import <QuartzCore/QuartzCore.h>

....

// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];
#import <QuartzCore/QuartzCore.h>

....

// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];
请持续率性 2024-09-05 05:04:44

为圆角添加以下内容:

self.yourUITextview.layer.cornerRadius = 8; 

Add the following for rounded corners:

self.yourUITextview.layer.cornerRadius = 8; 
半衾梦 2024-09-05 05:04:44

这是我使用的代码,用于在名为“tbComments”的 TextView 控件周围添加边框:

self.tbComments.layer.borderColor = [[UIColor grayColor] CGColor];
self.tbComments.layer.borderWidth = 1.0;
self.tbComments.layer.cornerRadius = 8;

如下所示:

在此处输入图像描述

简单易行。

Here's the code I used, to add a border around my TextView control named "tbComments" :

self.tbComments.layer.borderColor = [[UIColor grayColor] CGColor];
self.tbComments.layer.borderWidth = 1.0;
self.tbComments.layer.cornerRadius = 8;

And here's what it looks like:

enter image description here

Easy peasy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文