【问题标题】:Bordered UITextView有边框的 UITextView
【发布时间】:2010-04-15 16:31:57
【问题描述】:

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

【问题讨论】:

    标签: ios quartz-core


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

    【讨论】:

    • 我认为它真的不需要太多解释,视图是 UITextView,代码可以放在你设置视图的任何地方(awakeFromNib 或 viewDidLoad 是两个可能的地方)。由于没有给出代码,因此无法给出良好的响应上下文。
    • XCode 不允许我为图层设置边框。它说该层是只读的。我制作了 UIView(放置了一些元素)并尝试为该视图设置边框。尝试这样做self.myView.layer.borderWidth ...,但正如我所说,图层是只读的,所以图层没有任何方法或变量要设置
    • 你导入 QuartzCore 了吗?您也可以尝试从 self.myView 中取出一个 CALayer 并在其上设置borderWidth。它是可设置的。
    • 更多关于“layer”在 UIView 中只读的信息(layer 属性是,但您可以在 layer 中为视图设置值):stackoverflow.com/questions/12837077/…
    【解决方案2】:

    为圆角添加以下内容:

    self.yourUITextview.layer.cornerRadius = 8; 
    

    【讨论】:

      【解决方案3】:

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

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

      这就是它的样子:

      简单易懂。

      【讨论】:

        【解决方案4】:

        我将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

        【讨论】:

        • 很好的解决方案。这些图片是你的吗?我们可以免费使用它们吗?
        • 这些图像是“模仿”苹果自己的应用程序中使用的图像。事实上,我提取了它们并且做了很少的更改,如果有的话。使用风险自负。目标是模仿原生的外观和感觉,所以很难想出看起来有很大不同的东西。对于它的价值,我已经发布并批准了使用此图像的应用程序,没有问题。
        • png 图像文件似乎已被删除 - 我不打算用新的图像位置不断更新它,所以如果 jpg 不适合您,我深表歉意。我可以在评论要求时重新上传。
        • 附上图片效果最好:code resizableImageWithCapInsets:UIEdgeInsetsMake(28, 14, 28, 14)
        • WikiUpload 链接(用于两个图像)现在无法找到图像文件。 ;-(
        【解决方案5】:

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

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

        【讨论】:

        • #import
        【解决方案6】:

        我相信上面的答案是针对以前版本的 Swift 的。我用谷歌搜索了一下,下面的代码适用于 Swift 4。只需将它分享给任何可能受益的人。

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

        编码愉快!

        【讨论】:

        • 借助 Swift5 和深色模式,您甚至可以使用系统颜色:self.textViewName.layer.borderColor = UIColor.systemGray4.cgColor
        【解决方案7】:

        对于 Swift 编程,使用这个

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

        【讨论】:

        • 或者只是UIColor(white: 0.2, alpha: 1).CGColor
        【解决方案8】:

        这与原始 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
        }
        

        【讨论】:

          【解决方案9】:

          您可以从 Storyboard - Identity Inspector - User Defined Runtime Attribute 为 UITextView 添加边框

          【讨论】:

            【解决方案10】:

            从 iOS 8 和 Xcode 6 开始,我现在发现最好的解决方案是将 UITextView 子类化并将子类标记为 IB_DESIGNABLE,这样您就可以在故事板中查看边框。

            标题:

            #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
            

            然后在storyboard中拖出你的UITextView并将它的类设置为BorderTextView

            【讨论】:

              【解决方案11】:

              使它起作用的事情(除了遵循这里的答案)是添加borderStyle 属性:

              #import <QuartzCore/QuartzCore.h>
              ..
              
              phoneTextField.layer.borderWidth = 1.0f;
              phoneTextField.layer.borderColor = [[UIColor blueColor] CGColor];
              phoneTextField.borderStyle = UITextBorderStyleNone;
              

              【讨论】:

                【解决方案12】:

                只是一个小小的补充。如果你把边框弄宽一点,它会干扰文本的左侧和右侧。为了避免这种情况,我添加了以下行:

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

                【讨论】:

                  【解决方案13】:

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

                  myText.layer.borderColor = UIColor.lightGray.cgColor
                  
                  myText.layer.borderWidth = 1.0
                  

                  【讨论】:

                    【解决方案14】:

                    一个优雅的解决方案是在底部插入一个真正的 UITextField 并防止它随着内容滚动。这样,您甚至可以拥有正确的暗模式边框。 ?

                    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
                      }
                    }
                    

                    【讨论】:

                      【解决方案15】:

                      我在故事板中解决了这个问题,方法是在UITextView 后面放置一个完全禁用的UIButton,并使UITextView 的背景颜色为clearColor。这无需额外的代码或包即可工作。

                      【讨论】:

                      • 当然,这在 iOS 7 中不起作用,因为按钮没有边缘(按钮现在看起来像标签)
                      猜你喜欢
                      • 2012-07-10
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2022-06-29
                      • 1970-01-01
                      • 2012-06-04
                      • 2015-03-04
                      相关资源
                      最近更新 更多