我使用一个文视作一个评论的作曲家。

在属性检查我可以找不到任何东西就像一个边境式的财产所以,我可以使用一个圆rect,类似的东西 UITextField.

因此,问题是:我怎么风格 UITextView 像一个 UITextField 一个圆形的rect?

有帮助吗?

解决方案

有是你要选择的隐式的风格,它涉及到使用QuartzCore框架编写一些代码:

//first, you
#import <QuartzCore/QuartzCore.h>

//.....

//Here I add a UITextView in code, it will work if it's added in IB too
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];

//To make the border look very close to a UITextField
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];

//The rounded corner part, where you specify your view's corner radius:
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;

它仅适用于OS 3.0及以上,但现在我想它的事实上的平台呢。

其他提示

此代码行之有效对我来说:

    [yourTextView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
    [yourTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];
    [yourTextView.layer setBorderWidth: 1.0];
    [yourTextView.layer setCornerRadius:8.0f];
    [yourTextView.layer setMasksToBounds:YES];

夫特3版

设置在接口构建器的文本视图之后。

@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    textView.layer.cornerRadius = 5     
    textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
    textView.layer.borderWidth = 0.5  
    textView.clipsToBounds = true
}

夫特2.2版

@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    textView.layer.cornerRadius = 5     
    textView.layer.borderColor = UIColor.grayColor().colorWithAlphaComponent(0.5).CGColor
    textView.layer.borderWidth = 0.5  
    textView.clipsToBounds = true
}

编辑:你要导入

#import <QuartzCore/QuartzCore.h>

作为使用拐角半径。

试试这个将肯定工作

UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
txtView.layer.cornerRadius = 5.0;
txtView.clipsToBounds = YES;

作为 罗布了它设置,如果你想则需要边框宽度改变为2.0和颜色通过将下面的线为灰色边框颜色是作为UITextField类似

[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; 
[textView.layer setBorderWidth:2.0];

我想真正的交易,所以我加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];

这些是我使用的图像:

“在这里输入的图像描述” “在这里输入的图像描述”

一个解决方案是为添加下面的UITextView 的UITextField,使UITextView背景透明和禁用对UITextField任何用户交互。然后,在代码改变UITextField帧以类似的东西

self.textField.frame = CGRectInset(self.textView.frame, 0, -2);

您将有完全相同的外观为一个文本字段相同。

和由乔恩所建议的,你应该把这段代码内[UIViewController viewDidLayoutSubviews]在iOS 5.0 +

有关你必须使用自定义的(拉伸的)的背景图像的最佳效果。这也是UITextField的圆角的边框的绘制方式。

我发现做到这一点无需编程的方法之一是使文本框背景透明,然后将一个圆矩形按钮后面。确保更改按钮设置来禁用它并取消Disable adjusts image复选框。

您可能想看看我的库调用 DCKit

您会能够使圆角文本视图直接从UIView(以及文本字段/按钮/纯Interface Builder):

“DCKit:接壤的UITextView”

它也有许多其他有用的功能,例如与验证文本领域,具有边界,虚线边框,圆和发际的观点等控制。

我知道已经有很多答案,这一个,但我真的没有发现任何他们有足够的(至少在斯威夫特)。我想,所提供的完全相同的边框,为UITextField(不是近似一个看起来有点像它看起来的权利,但一个看起来完全一样,将永远看起来完全一样)的解决方案。我需要使用的UITextField到后的UITextView为背景,但不希望有创建单独的每一次。

下面的解决方案是供给它自己的UITextField用于边框一个UITextView。这是我的完整的解决方案的下调版本(增加以类似的方式向UITextView的“占位”的支持),并张贴在这里: https://stackoverflow.com/a/36561236/1227119

// This class implements a UITextView that has a UITextField behind it, where the 
// UITextField provides the border.
//
class TextView : UITextView, UITextViewDelegate
{
    var textField = TextField();

    required init?(coder: NSCoder)
    {
        fatalError("This class doesn't support NSCoding.")
    }

    override init(frame: CGRect, textContainer: NSTextContainer?)
    {
        super.init(frame: frame, textContainer: textContainer);

        self.delegate = self;

        // Create a background TextField with clear (invisible) text and disabled
        self.textField.borderStyle = UITextBorderStyle.RoundedRect;
        self.textField.textColor = UIColor.clearColor();
        self.textField.userInteractionEnabled = false;

        self.addSubview(textField);
        self.sendSubviewToBack(textField);
    }

    convenience init()
    {
        self.init(frame: CGRectZero, textContainer: nil)
    }

    override func layoutSubviews()
    {
        super.layoutSubviews()
        // Do not scroll the background textView
        self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
    }

    // UITextViewDelegate - Note: If you replace delegate, your delegate must call this
    func scrollViewDidScroll(scrollView: UIScrollView)
    {
        // Do not scroll the background textView
        self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
    }        
}
  

我发现做到这一点无需编程的方法之一是使文本框背景透明,然后将一个圆矩形按钮后面。确保更改按钮设置来禁用它,并取消禁止调节图像的复选框。

试过Quartzcore代码,并发现它在我的旧3G(I用于测试)而造成的滞后。不是一个大问题,但如果你想尽量包容不同的iOS和硬件,我建议Andrew_L的回答以上 - 或者是让自己的图像,并据此适用

有是是相同的用于iPhone的消息应用程序发送文本消息的UITextView很大的背景图像。你需要Adobe Illustrator的得到和修改。 iphone UI矢量元素

#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad{
  UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
  textView.layer.cornerRadius = 5;
  textView.clipsToBounds = YES;
  [textView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
  [textView.layer setBorderColor: [[UIColor grayColor] CGColor]];
  [textView.layer setBorderWidth: 1.0];
  [textView.layer setCornerRadius:8.0f];
  [textView.layer setMasksToBounds:YES];
  [self.view addSubView:textview];
}

您可以创建一个文本字段,不接受文本视图像这样的顶部的事件:

CGRect frameRect = descriptionTextField.frame;
frameRect.size.height = 50;
descriptionTextField.frame = frameRect;
descriptionTextView.frame = frameRect;
descriptionTextField.backgroundColor = [UIColor clearColor];
descriptionTextField.enabled = NO;
descriptionTextView.layer.cornerRadius = 5;
descriptionTextView.clipsToBounds = YES;

如果你想保持你的控制器代码干净,你也可以继承的UITextView像下面,并在Interface Builder中更改类的名称。

RoundTextView.h

#import <UIKit/UIKit.h>
@interface RoundTextView : UITextView
@end

RoundTextView.m

#import "RoundTextView.h"
#import <QuartzCore/QuartzCore.h>
@implementation RoundTextView
-(id) initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.333] CGColor]];
        [self.layer setBorderWidth:1.0];
        self.layer.cornerRadius = 5;
        self.clipsToBounds = YES;
    }
    return self;
}
@end

我不认为这是可能的。但可以用1个部分和1个空细胞做UITableView(分组),并使用它作为您UITextView的容器。

这是一个古老的问题,我也是寻找这问题的答案。luvieeres'的答复是100%正确的,后来抢劫添加一些代码。这就是优良,但是我发现的第三方 另一个问题的答案 这似乎非常有帮助我。我不只寻找类似的看的 UITextFieldUITextView, 我还搜查了多的支持。 ChatInputSample 满意两者。这就是为什么我认为这个第三方可能有助于其他人。还感谢铁木尔,他提到的该开放源码在 在这里,.

在iOS7以下匹配(至少到我的眼睛)的UITextField边界完美:

textField.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor];
textField.layer.borderWidth = 0.5;
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;

有是不需要输入任何特殊。

感谢@uvieere和@hanumanDev其答案去我几乎没有:)

下面是我的解决方案:

- (void)viewDidLoad {
    [super viewDidLoad];


    self.textView.text = self.messagePlaceholderText;
    self.textView.layer.backgroundColor = [[UIColor whiteColor] CGColor];
    self.textView.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.3] CGColor];
    self.textView.layer.borderWidth = 0.5;
    self.textView.layer.cornerRadius = 5.5f;
    self.textView.layer.masksToBounds = YES;
    self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if (textView == self.tvMessage) {
        // Delete placeholder text
        if ([self.textView.text isEqualToString:self.messagePlaceholderText]) {
            self.textView.text = @"";
            self.textView.textColor = [UIColor blackColor];
        }
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView == self.tvMessage) {
        // Write placeholder text
        if (self.textView.text.length == 0) {
            self.textView.text = self.messagePlaceholderText;
            self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
        }
    }
}

如何只:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 32)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:textField];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top