每行

时间:2016-05-04 11:20:54

标签: ios swift uitextview border line

我正在开发一个应用程序,其中textView的每行都有底部边框,我在google和StackOverflow上找不到这个问题的答案。所以我决定问一个问题,我有这个文本视图:My Textview

我希望它看起来像这样:Original Textview

我是以编程方式进行的,所以请不要给我讲故事板示例,谢谢

2 个答案:

答案 0 :(得分:1)

var textview:UITextView=UITextView()
for var i:Int = 20 ; i <= Int(textview.frame.size.height) ; i = i + 20 // set 20 you line distance .. change your chooice
{
    let border = CALayer()
    border.borderColor = UIColor.grayColor().CGColor
    border.frame = CGRectMake(0, CGFloat(i), textview.frame.size.width*1.5 , 1.0)
    border.borderWidth = 1.0
    textview.layer.addSublayer(border)
    textview.layer.masksToBounds = true
}

答案 1 :(得分:0)

您可以使用此代码

-(void)setBorderView:(UITextField*)textField
{
    UIView *borderView = [[UIView alloc]init];
    borderView.backgroundColor = [UIColor clearColor];
    CGRect frameRectEmail=textField.frame;


    NSLogVariable(textField);
    borderView.layer.borderWidth=1;
    borderView.layer.borderColor=[customColor colorWithHexString:@"8c8b90"].CGColor;
    borderView.layer.cornerRadius=5;
    borderView.layer.masksToBounds=YES;



    UIView *topBorder = [[UIView alloc]init];
    topBorder.backgroundColor = [customColor colorWithHexString:@"1e1a36"];
    CGRect frameRect=textField.frame;
    frameRect.size.height=CGRectGetHeight(textField.frame)/1.5;
    topBorder.frame = frameRect;
      frameRectEmail.size.width=frameRect.size.width;
    [borderView setFrame:frameRectEmail];

    [_textFieldBackGroundView addSubview:borderView];
    [_textFieldBackGroundView addSubview:topBorder];
    [_textFieldBackGroundView addSubview:textField];

}