2013-11-28 54 views
3

我有一個UITextView,我在其圖層上設置邊框寬度,邊框顏色和圓角半徑屬性,外部看起來很棒。但是,邊界的內部沒有像外部那樣的圓角,並且看起來很有趣。有沒有辦法繞過邊界的內角?CALayer內邊框圓角半徑

編輯: 這是我在initWithFrame:方法使用的代碼:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = UIColorFromRGB(0xdedede); 
     self.layer.cornerRadius = kTextFieldCornerRadius; 
     self.layer.borderColor = UIColorFromRGB(0xD4974C).CGColor; 
     self.layer.borderWidth = 3.0f; 
     self.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0f]; 
     [self setClipsToBounds:YES]; 
     [self.layer setMasksToBounds:YES]; 
    } 
    return self; 
} 

下面是什麼樣子,現在的截圖:預期外邊角圓潤

通知,但邊框的內角是尖的而不是圓角的。這就是我想要解決的問題。謝謝你的幫助!

+4

請顯示您的代碼和快照textview –

回答

9

嘗試設置此,

[txtView  setClipsToBounds:YES]; //Confirms subviews are confined to the bounds of the view 
[txtView.layer setMasksToBounds:YES]; //Confirms sublayers are clipped to the layer’s bounds 

編輯

大概kTextFieldCornerRadius的值設置爲低,你的情況。

如果我設置kTextFieldCornerRadius = 7;看我能得到完美的輸出。

enter image description here

儘量增加你的半徑值。

+0

試過了,沒有運氣。查看上面的代碼和屏幕截圖。 – Mason

+0

@梅森看到更新 –

+0

我試過了,沒有工作。 :( –

1

導入QuartzCore框架和添加的下面幾行代碼:

Objective - C的

UIView *yourView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)]; 
yourView.layer.borderColor = [UIColor redColor].CGColor; 
yourView.layer.borderWidth = 10.0f; 
yourView.layer.cornerRadius = 20.0f; 

[yourView setClipsToBounds:YES]; 
[yourView.layer setMasksToBounds:YES]; 

SWIFT - 3.0.1(操場代碼)

//: Playground - noun: a place where people can play 

import UIKit 
import PlaygroundSupport 
import QuartzCore 

let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 100.0)) 

containerView.backgroundColor = UIColor.white 
containerView.layer.borderWidth = 10 
containerView.layer.borderColor = UIColor.red.cgColor 
containerView.clipsToBounds = true 
containerView.layer.masksToBounds = true 
containerView.layer.cornerRadius = 20 

PlaygroundPage.current.liveView = containerView 
PlaygroundPage.current.needsIndefiniteExecution = true 

OUTPUT:

OUTPUT :

重要:

確保cornerRadiusborderWidth更大。否則你將無法看到差異。